0

Why is this syntax illegal in C?

int *a = {1,2,3};

Aren't arrays and pointers the same thing? Why would this work and not the above?

int a[] = {1,2,3};
int *b = a;
  • 2
    Obviously they are not the same ;). You can convert an array into a pointer but not the other way round. Also `sizeof(a)` and `sizeof(b)` will give you very different results in your second example. – mrksngl May 29 '20 at 15:42
  • "Aren't arrays and pointers the same thing?"; No, they're actually slightly different (a difference which is further muddied by the fact that arrays will oft _decay_ to a pointer to the first element thereof). – kopecs May 29 '20 at 15:43
  • As far as I understand it, arrays are static whereas pointers are dynamic which is why that doesn't work – txk2048 May 29 '20 at 15:46
  • 1
    Arrays and pointers are quite different, even though arrays are automatically converted to pointers under certain circumstances. In your case, you're attempting to create an anonymous array, then initialize a pointer variable to point to it (actually, to its first element). There is a syntax for doing so: `int *a = (int []) {1, 2, 3};` will work. – Tom Karzes May 29 '20 at 16:06
  • @TomKarzes: Yes, but you have to be very careful with that. `a` ends up pointing to an object with automatic lifetime, so you cannot, for example, return `a` from a function. – rici May 29 '20 at 16:09
  • @rici It has the same storage class that an array would have, i.e. it's the same as `int anon[] = {1, 2, 3}, *a = anon;` But at the file level, the storage would presumably be permanent. – Tom Karzes May 29 '20 at 16:11
  • @Tom: Yes, at file scope it's a static object. But I often see it used in function scope where it turns into a dangling pointer. – rici May 29 '20 at 16:20
  • 1
    Reopened because the question explicit asks about the initialization syntax, which is a distinct question from whether arrays are pointers. – Eric Postpischil May 29 '20 at 16:20

1 Answers1

0

Why is this syntax illegal in C?

In int a[] = {1,2,3};, the {1,2,3} is a syntax for providing initial values of the things being initialized. A list of values in braces is a general notation for listing initial values. It is not a notation for representing an array.

The declaration says a is an array, but that is just in its int a[] part. The {1,2,3} does not indicate an array—it just provides a list of values, and it could be used for non-array objects, such as providing values for the members of a structure.

In int *a = {1,2,3};, the int *a part defines a to be a pointer. A pointer just has one value in it—the address of something (or null). So it does not make sense to initialize it with multiple values. To initialize a pointer, you need to provide an address for it to point to.

There is a notation in C for representing an array of given values. It is called a compound literal, and can be used for types in general, not just arrays. Its syntax is “(type) { initial values }”. So we can create an array with (int []) {1, 2, 3}. A pointer could be defined and initialized to point to this array with int *a = (int []) {1, 2, 3};. However, this should be done with care. Such an array is temporary (has automatic storage duration that ends when execution of its associated block ends) when defined inside a function, so the pointer will become invalid when the function returns (or before, if it is in an inner block).

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312