-2
#include <stdio.h>
int main(){
  int *a = {1,2,3,4,5};
  printf("a:%x &a:%x\n",a,&a);
  return 0;
}

I compiled this program with GCC. The output of a is 1, and the output of &a is an address. What GCC did to int *a = {1,2,3,4,5}? Did GCC treat it as an array or a pointer that points to an array or something else?

pynexj
  • 19,215
  • 5
  • 38
  • 56
markable
  • 79
  • 7
  • 1
    don't post pictures of code, post the actual code. – Joe Mar 23 '18 at 01:55
  • Not because we can't read image of code, but because they are code and should be posted as such. Someone may also want to copy-and-paste it to run it. – user202729 Mar 23 '18 at 01:57
  • [Array automatically decay into pointer](https://stackoverflow.com/questions/1461432/what-is-array-decaying) in most cases. <--- possible dupe – user202729 Mar 23 '18 at 01:58
  • `int *a = {1,2,3,4,5};` really? – sg7 Mar 23 '18 at 02:02
  • comma works as an operator here. that's why you get a=1 as a={1,2,3,4,5} becomes equivalent to “(a = 1),2,3,4,5" . and *a is pointing to an integer. – itsrajon Mar 23 '18 at 02:05
  • A pointer to an integer can also be a pointer to the first element of an array. The compiler definitely won't just ignore curly braces. This is not the problem. – eesiraed Mar 23 '18 at 02:54
  • @Pusku there is no comma operator inside curly braces – M.M Mar 23 '18 at 06:22
  • @user202729 there are no arrays in this code – M.M Mar 23 '18 at 06:24
  • After some "what does this code do" like [this one](https://stackoverflow.com/questions/13603286/how-is-int-main-valid-c) it gets quickly boring. In particular this case the compiler basically already explained it. – user202729 Mar 23 '18 at 06:29

2 Answers2

1

I did not refer to the C standards but you can see how gcc handles this from the compile warning messages:

[STEP 101] # cat foo.c
int main()
{
    int * a = {1, 2};
    return !!a;
}
[STEP 102] # gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:3:16: warning: initialization makes pointer from integer
without a cast [-Wint-conversion]
     int * a = {1, 2};
                ^
foo.c:3:16: note: (near initialization for ‘a’)
foo.c:3:19: warning: excess elements in scalar initializer
     int * a = {1, 2};
                   ^
foo.c:3:19: note: (near initialization for ‘a’)
[STEP 103] #

UPDATE:

Just took a look at the C99 standard and found this in section 6.7.8 Initialization:

The initializer for a scalar shall be a single expression, optionally enclosed in braces.

pynexj
  • 19,215
  • 5
  • 38
  • 56
1

The code is a constraint violation as an initializer for a non-aggregate can only contain 1 element.

GCC has an "extension" to ignore excess initializers, so it treats the code as int *p = 1;. This is also a constraint violation, because an integer cannot be assigned to a pointer. But gcc has another "extension" to treat such code as int *p = (int *)1;. So you end up with a pointer to address 1.

and the output of &a is an address.

&a is the address in memory of the variable a, this has nothing to do with what value is stored in a.

M.M
  • 138,810
  • 21
  • 208
  • 365