0
int *ar[3];
int x;
for (x == 0; x < 3; ++x)
    printf("AR[%d]: %d\n", x, ar[x]);

this returns

AR[0]: 0

AR[1]: 0

AR[2]: 4196432

Community
  • 1
  • 1
  • 5
    because it is not empty, it is uninitialized. – mch Nov 18 '15 at 13:07
  • 2
    Possible duplicate of [What is the default value of a char in an uninitialized array, in C?](http://stackoverflow.com/questions/24797860/what-is-the-default-value-of-a-char-in-an-uninitialized-array-in-c) – Gábor Bakos Nov 18 '15 at 13:08
  • 2
    `ar` is **NOT** an array of `int`. Its values are of type `int *` (pointer to int) and you need to use `"%p"` (and cast to `(void*)`) in the `printf()` call ... or ... redefine the array as array of ints: `int ar[3];` – pmg Nov 18 '15 at 13:11
  • 5
    Is "x == 0" a typo in the post? Your code should have "x = 0" or it evaluates to a 0 or 1, depending if uninitialized x is 0 or not. Follow the link from Gábor Bakos on this. – Gilbert Nov 18 '15 at 13:18
  • You should use post increment as opposed to pre, ++x is 1 before the first pass of the loop - AR[0] isn't being initialised - use x++ – Nunchy Nov 18 '15 at 13:22
  • 4
    @Nunchy: That's wrong. The final clause of a `for(;;)` loop happens _after_ the loop has executed. It doesn't matter whether you use `++x` or `x++`. – TripeHound Nov 18 '15 at 13:38

1 Answers1

1

"int *ar[3]" means array of pointers, its element is a pointer to int, and there is no assignment for this array, that means its element might be any garbage. BTW, the type of ar[x] is pointer, if you want to print ar[x], you should use "%p" instead of "%d", otherwise there is a vast from %p to %d, and the value maybe is not you expect.

sirbingo
  • 31
  • 3