about arrays initialization

Maciej Bliziński maciej@opencsw.org
Sat Aug 27 14:41:00 GMT 2011


2011/8/27 uulinux <uulinux@gmail.com>:
> localhost array # gcc -o main main.c
> localhost array # ./main
>        1       2       3       -1218524971     -944870872
> ===============================================
>
>   I want to know, what is wrong with it?

I'm not an expert, but it seems to me that in the expression "int
a[5]={1, 2, 3, a[2], a[0]+a[1]};" when you say e.g. a[2], you are
referring an uninitialized array. Your code, as far as initialization
goes, would be equivalent to:

int b[5];
int a[5]={1, 2, 3, b[2], b[0]+b[1]};

In this example, it's clear what's wrong.  Your code has the same
problem, but slightly obfuscated.

If you try to do the same in Python, you get:

>>> a = [1, 2, 3, a[2], a[0]+a[1]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

Which is exactly the same problem you get in C, but exposed better.

Maciej



More information about the Gcc-help mailing list