473,322 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Array Declaration

hai
i am mithra. i have one doubt in c Array declaration.
<CODE>
#include<stdio.h>
main()
{
int a[10];int i;
for(i=0;i<20;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<20;i++)
{
printf("%d",a[i]);
}
}
</CODE>

In this code i just declared 10 elements of array. but i gave 20 elements as an input. it can accept it. it does not generate any errors. it just print the 20 elements so when the memory is allocated? is it in declaration or not

please clarify my doubt
Jul 21 '07 #1
3 2131
JosAH
11,448 Expert 8TB
hai
i am mithra. i have one doubt in c Array declaration.
<CODE>
#include<stdio.h>
main()
{
int a[10];int i;
for(i=0;i<20;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<20;i++)
{
printf("%d",a[i]);
}
}
</CODE>

In this code i just declared 10 elements of array. but i gave 20 elements as an input. it can accept it. it does not generate any errors. it just print the 20 elements so when the memory is allocated? is it in declaration or not

please clarify my doubt
You were just lucky; your program used part of a stack that didn't belong to your
process. You were just lucky that nothing else needed that part of the stack.
Crossing array boundaries causes undefined behaviour. Anything could have
happened, like daemons flying out of your nose ;-)

kind regards,

Jos
Jul 21 '07 #2
Meetee
931 Expert Mod 512MB
You were just lucky; your program used part of a stack that didn't belong to your
process. You were just lucky that nothing else needed that part of the stack.
Crossing array boundaries causes undefined behaviour. Anything could have
happened, like daemons flying out of your nose ;-)

kind regards,

Jos
But, when I am running the same program, after entering 19 values, it gives segmentation fault. How does the same program run with different behaviours?? Can it change the behaviour of array?

Regards
Jul 21 '07 #3
JosAH
11,448 Expert 8TB
But, when I am running the same program, after entering 19 values, it gives segmentation fault. How does the same program run with different behaviours?? Can it change the behaviour of array?

Regards
An excellent example of undefined behaviour. Anything could have happened.

kind regards,

Jos
Jul 21 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: User | last post by:
Hi, This is very basic, It may be a repost, if so I'm sorry. The problem is that this declaration : Private strMyArray(100) As String will create an array of string with a length of 101,...
1
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
5
by: pandapower | last post by:
Hi, I know about the equivalence of pointer and arrays.But my doubt comes when its for multidimentional arrays.I have read the C faq but still have some doubts. Suppose I have a declaration as...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
9
by: joshc | last post by:
Hi, I have an array defined in one file with an intializer as follows: int arr = {0, 1, 2, 3}; I have a declaration of the array in another file as follows: extern int arr;
19
by: DarelRex | last post by:
Is it possible to pass a 2-D, statically defined array? Here's a 1-D example that won't work: void foo() { int myArray ; bar(myArray); } void bar(int *arr) {
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
18
by: mdh | last post by:
>From p112 ( K&R). Given an array declared as static char arr= { { 0,1,........},{0,1,.....}}; let arr be passed as an argument to f. f( int (*arr) ) {....} It is noted that the...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.