about arrays initialization
Bob Plantz
plantz@cds1.net
Sun Aug 28 19:35:00 GMT 2011
Ah, yes, I missed the i=0 statement. And now I see why it is done twice.
Your code sets i=0 twice:
int i = 0;
and
for (i=0;....
The order of assigning values to the elements of the array makes sense,
but it's not part of the C language standard. I would not be surprised
if some other C compiler does as you suggest with this code. But to
ensure that your code is portable, you need to do (notice my comment to
help the maintenance programmer):
===============================================
#include <stdio.h>
int main()
{
int a[5]={1, 2, 3};
int i;
// Finish initializing array
a[3] = a[2];
a[4] = a[0] + a[1];
for (i=0; i<5; i++) {
printf("\t%d\n", a[i]);
}
return 0;
}
===============================================
By the way, it is my personal programming philosophy that one should NOT
do arithmetic operations during variable initialization.
Also, I like to use symbolic names and #defines. Again, it helps the
maintenance programmer. (I have found that this is often me, a few weeks
later.) So the above code might become something like (depending on what
the numbers actually represent):
===============================================
#define nCats 1
#define nDogs 2
#define nBirds 3
#define nWinged nBirds
#define nFourLegged nCats+nDogs
#include <stdio.h>
int main()
{
int myCreatures[5]={nCats, nDogs, nBirds, nWinged, nFourLegged};
int i;
for (i=0; i<5; i++) {
printf("\t%d\n", myCreatures[i]);
}
return 0;
}
===============================================
On 08/28/2011 12:53 AM, uulinux wrote:
> Dear Bob plantz:
>
> Thank you very mach. You are very enthusiastic and friendly, and your
> analysis is very good. The C code is as following:
> ===============================================
> #include <stdio.h>
> int main()
> {
> int a[5]={1, 2, 3, a[2], a[0]+a[1]};
> int i=0;
>
> for (i=0; i<5; i++) {
> printf("\t%d", a[i]);
> }
> return 0;
> }
> ===============================================
> So, please read the following again.
> >> movl %eax, 36(%esp) # a[3] = a[2] (previously computed above]
> >> movl %edx, 40(%esp) # a[4] = a[0] + a[1] (previously computed above]
> >> movl $0, 44(%esp) # a[5] = 0
> >> movl $0, 44(%esp) # a[5] = 0
> > # I have no idea why 0 is stored beyond the end of the array or why
> it is done twice. But the assignment to the elements of the array
> occur as I have always expected in C.
> It is not a[5] = 0. It is i=0; But I also have no idea about why it
> is done twice.
>
> I have known the reason of this issue is "a[3], a[4]" has not
> evaluated before the assignment occurred. So the result of this action
> is not fixed? And the result of this action is determined by the
> compiler. But there is still a question in my head. Why gcc put
> "a[3]=a[2]; a[4]=a[0]+a[1];" operation in the front of "a[0]=1;
> a[1]=2; a[2]=3;"? Is not the array has initialized from a[0] to a[n]?
> I just wonder why not put "a[0]=1; a[1]=2; a[2]=3;" operation in the
> front of "a[3]=a[2]; a[4]=a[0]+a[1];".
>
> Is there a method to achieve âa[3] = a[2]; a[4]=a[0]+a[1]â for
> global variable. So, I just need to update the value of a[0]â¦a[1] when
> the value should be changed. I think it is useful when the arithmetic
> expression is very complex. The value of some elements in a array will
> be auto updated by compiler, just like this:
> ===============================================
> static int a[6]={1, 2, 3, a[2], a[0]+a[1], sin(a[4])}; //
> gcc-4.4.5 report error !
>
> #include <stdio.h>
> int main()
> {
> int i=0;
>
> for (i=0; i<6; i++) {
> printf("\t%d", a[i]);
> }
> return 0;
> }
> ===============================================
More information about the Gcc-help
mailing list