Re: about arrays initialization

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2011/8/27 uulinux <uulinux@xxxxxxxxx>:
> 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



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux