EmbLogic's Blog

array assignment problem

#include
#define size 4
int main (void)
{
int val1=44;
int arr[size];
int val2=88;
int i;

printf(“value1 = %d,value2= %d\n”,val1,val2);
for(i=-1;i<=size;i++)
arr[i]=2*i+1;
printf("value1 =%d,value2=%d\n",val1,val2);
for(i=-1;i<7;i++)
printf("%2d %d\n",i , arr[i]);
printf("value1 =%d,value2=%d\n",val1,val2);
printf("value1 =%d,value2=%d\n",val1,val2);
return 0;
}

value1 = 44,value2= 88
-1 -1
0 1
1 3
2 5
3 7
4 9
5 44
6 6
value1 =44,value2=9
in the result why does the value of value2 has changed to 9
and
in array arr[i] what i sthe calculation going on for arr[6] and arr[7].

2 Responses to array assignment problem

  1. (1)according to me the value of value2 should not be changed ….
    (2)the value printed for arr[6] and arr[7] are garbage values.there is no calculation taking place their….

  2. msiddarth says:

    he compiler doesn’t check to see whether the indices are valid.
    * The result of using a bad index is, in the language of the C standard, undefined.
    * That means when we run the program, it might seem to work, it might work oddly, or it might abort.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>