2

I have a question about C syntax, how is it possible that this:

int a[ ][2] = { 1, 2, 3, 4 };

is legal why this is not:

int a[] = { 1, 2, 3, 4 };
int b[ ][2] = a;

or even:

int a[] = { 1, 2, 3, 4 };
int *b[2] = a;

is not legal.

What it is so hard from the pointer-math point of view that it cannot handle this by itself? Is it just a problem with the Grammar or the compilers can't infer the correct memory step-sizes?

gcc error:

main.c:14:19: error: invalid initializer
     int b[ ][2] = a;
Luca
  • 1,270
  • 1
  • 18
  • 34
  • 11
    `C` or `C++`? (They are different languages with different restrictions!) – hellow Oct 09 '18 at 14:27
  • it is illegal on both compilers – Luca Oct 09 '18 at 14:28
  • 8
    Assignment and initialisation are completely different operations. –  Oct 09 '18 at 14:32
  • I add to @Neil Butterworth comment: also memory allocation. In c `int b[][2]; strcpy(b,a);` – Phiber Oct 09 '18 at 14:36
  • You are asking us why the designers of these languages chose to allow initialization from a brace-enclosed list, but not from an array? – ad absurdum Oct 09 '18 at 14:36
  • there is always a reason for a design choice, and i don't see any good reason for this one. – Luca Oct 09 '18 at 14:37
  • As you can see, the people here react pretty sensitive if you tag your question with `c` and `c++`. [Corresponding meta post](https://meta.stackoverflow.com/questions/252430/disallow-the-tagging-of-questions-with-both-c-and-c-tags). You could split your question in two or focus only on one language to avoid this. – izlin Oct 09 '18 at 14:40
  • 1
    now it is just C. – Luca Oct 09 '18 at 14:42
  • The point is, any reasons for such design choices can only be provided by the original language designers. Other answers will likely be only conjecture, speculation, and opinion. – ad absurdum Oct 09 '18 at 14:46
  • "i don't see any good reason for this one." While I don't mean to be impolite, the reason is irrelevant. Nobody is going to change this behavior in a language that's been around for decades. The author died in 2011, so it's not likely you'll ever get a definitive answer. – Terry Carmen Oct 09 '18 at 14:48
  • @DavidBowling Sometimes, there is a strong technical reason, like https://stackoverflow.com/questions/20046429/casting-pointer-to-array-int-to-int2 In this case I gues the problem is that `{...}` is initializer, when it's split in two lines the types do not match. – luk32 Oct 09 '18 at 14:48
  • @Luca you claimed that `int a[] = { 1, 2, 3, 4 };` is illegal, but it is not. – Weather Vane Oct 09 '18 at 14:51
  • 1
    IMHO the legality of `int a[ ][2] = { 1, 2, 3, 4 };` is a red herring, it's completely irrelevant. That's just a syntax to initialize array. The questions is why can't you assign array to matrix/array. Just because it looks similar or seems like you can remove `a` as some temporary thing doesn't mean the code is equivalent. – luk32 Oct 09 '18 at 14:56
  • @luk32 -- initializers get special treatment. It seems possible that the designers could have chosen to allow an initializer expression to use arrays on the right as well as brace-enclosed lists; why didn't they? Only they know the answer, but this would _not_ be a case of assignment. – ad absurdum Oct 09 '18 at 14:59
  • "What it is so hard from the pointer-math point of view that it cannot handle this by itself?", C has never been math oriented. – Stargateur Oct 09 '18 at 15:02
  • This question is not opinion-based at all, stop casting close votes for that reason. It is a valid question and the answer is: that's just how the language is defined, period. Good answers can cite the C grammar, the rules of simple assignment and the C rationale. There is no room for opinions at all. – Lundin Oct 09 '18 at 15:03
  • Actually I found a dupe. – Lundin Oct 09 '18 at 15:05
  • @Lundin -- there is no room for opinion that you can't initialize an array from another array, but why is it so? The trivial answer is that that Standard says so, but that isn't a real question. There is only opinion to answer why the language was designed this way, in the absence of the original designers. – ad absurdum Oct 09 '18 at 15:05
  • 1
    @DavidBowling The linked dupe offers some insight of the history of C. Unfortunately with some dead links to Bell Labs, likely Dennis Ritchie's original rationales etc. – Lundin Oct 09 '18 at 15:06
  • @Luca: First of all, remember that C is a product of the early 1970s, and it still shows in a number of places. For a number of reasons, arrays are treated differently from other types such that array expressions are automatically converted to pointer expressions (which is why you can't initialize an array with an array in a declaration), and array expressions may not be the target of an assignment. *All* aggregate types (arrays, structs, unions) use brace-enclosed lists for initializers - character arrays may use string literals for initializers. – John Bode Oct 09 '18 at 15:36
  • @Lundin -- [here is another link to Ritchie's paper](https://www.bell-labs.com/usr/dmr/www/chist.html) that seems to work. – ad absurdum Oct 09 '18 at 15:38

1 Answers1

1

The explanation is very simple.

Two main reasons:

  1. In the C you cant assign arrays. The only complex types where assignment is allowed in C are structs and unions.
  2. The initialization of the global variables requires constant expression.
0___________
  • 60,014
  • 4
  • 34
  • 74