Hello All,
I have written the following code. I have declared 2 arrays t and s both of size of 4 integers and two integer variables u and i. Please pay attention to the order they have been declared. In the following code, I have printed out the addresses of each array element and variable in the opposite sequence in that they have been declared (s, i, u, t).
Code:
# include <stdio.h>
# include <string.h>
int main()
{
int t[4] = {10, 30, 50, 20}, u = 5;
int i;
int s[4] = {90, 44, 23, 76};
for ( i = 0; i < 4; i++ ) {
printf ("s[%d] = %u, %d\n", i, s + i, s[i]);
}
printf ("i = %u, %d\n", &i, i);
printf ("u = %u, %d\n", &u, u);
for ( i = 0; i < 4; i++ ) {
t[i] = i;
printf ("t[%d] = %u, %d\n", i, t + i, t[i]);
}
return 0;
}
The output of the program is as follows:
Code:
s[0] = 3216459112, 90
s[1] = 3216459116, 44
s[2] = 3216459120, 23
s[3] = 3216459124, 76
i = 3216459128, 4
u = 3216459132, 5
t[0] = 3216459136, 0
t[1] = 3216459140, 1
t[2] = 3216459144, 2
t[3] = 3216459148, 3
It means they have been allocated memory in this order only(reverse of the order in which they have been declared). Can It change on different compiler or OS?
Operating System: Linux (Mandriva 2010)
Compiler: gcc 4.4.1
Thanks and Best Regards,
Aakash Johari