I am getting an error in initialization of array of integer pointer. Though the issue seems simple I am unable to understand what's wrong in this. I am new in C programming.

- 133,132
- 16
- 183
- 261

- 667
- 5
- 3
-
5It's legal C (well, except the void main). Unfortunately your compiler of choice is quite dated and may not accept all possible code that is valid today. – StoryTeller - Unslander Monica Mar 13 '18 at 06:15
-
1You need some serious upgrade. – haccks Mar 13 '18 at 06:15
-
Just to illustrate further, here's your two declarations in a more modern compiler (gcc 6.3) https://ideone.com/K3NGXM – StoryTeller - Unslander Monica Mar 13 '18 at 06:17
-
5That image made me nostalgic. – taskinoor Mar 13 '18 at 06:17
-
`I am new in C programming...` Then start correct. First thing, DO NOT use TC anymore, it's pretty invalid now a days. – Sourav Ghosh Mar 13 '18 at 06:22
-
1Please don't post images of code! Include the text of the code directly in the question! – Jonathan Leffler Mar 13 '18 at 06:24
-
3Most likely, that compiler is older than you are. Get one that dates from the current millennium — preferably the current decade (the last year or so would be best). – Jonathan Leffler Mar 13 '18 at 06:27
-
2@JonathanLeffler I was tempted to downvote because image of code, but in this case the image explained the problem at glance, so now I am confused... – Matteo Italia Mar 13 '18 at 06:36
-
1@MatteoItalia: I can sympathize with that. The [tag:turbo-c] tag also gives the information. It is sad that there seem to be teaching environments where TC is still the torture device^H^H^H^H^H^H^H^H^H^H^H^H^H^H compiler system of choice. – Jonathan Leffler Mar 13 '18 at 06:45
-
Why use such an outdated, well, everything? – A. Smoliak Mar 13 '18 at 07:16
-
Wow, I never thought about those 3 bars in the upper left corner before. And here I thought "hamburger menues" were a thing created by web design hipsters! IIRC that menu gave various "window" options to the IDE itself, much like clicking the upper left corner in a Windows program nowadays. Say what you will about this compiler today, but it was quite ahead of its time back then. – Lundin Mar 13 '18 at 10:31
-
Possible duplicate of [Illegal Initialization In C Program](https://stackoverflow.com/questions/39011060/illegal-initialization-in-c-program) – Michael Petch Mar 13 '18 at 12:10
-
This is because variable on the stack isn't constant (not computable at time of compiling). If you had moved the array `a` outside the function to global scope (or marked it `static`) where it would have a fixed address in memory it should work. – Michael Petch Mar 13 '18 at 12:39
-
@StoryTeller : In the case of the initialisers, Turbo-C's interpretation is correct for C90. An array is an aggregate type and per the spec it has to use constant expressions.The spec also defines an address constant for NULL and an address to something of static storage - but that doesn't include the address of temporary variables (non-static variables). You can even get GCC to give you a warning about this if you use `gcc -std=c90 -pedantic`. C99 changed the rules about initialisers to allow for it. – Michael Petch Mar 13 '18 at 13:22
2 Answers
The C90 standard said (in §6.5.7):
All the expressions in an initializer for an object that has static storage duration or in an initializer list for an object that has aggregate or union type shall be constant expressions.
In context, p
'has aggregate type', and the addresses of the array elements of a
are not constants, so C90 says that initialization is not allowed. That's why your compiler rejects that code.
C99 relaxes that restriction. The corresponding paragraph (§6.7.8 ¶4) says:
All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.
Do yourself a favour and get a compiler that dates from the current millennium — it should be one that implements C11 if at all possible.

- 730,956
- 141
- 904
- 1,278
-
Nice point, then using `static int a[] ..` does the trick, isn't it? _Do yourself a favour and get a compiler that dates from the current millennium_ - I don't know why but lot of people still uses Turbo C for teaching. – David Ranieri Mar 13 '18 at 06:59
-
[Agreed!](https://stackoverflow.com/questions/49249410/error-in-static-initialization-of-array-of-integer-pointers-in-c/49249739?noredirect=1#comment85503054_49249410) – Jonathan Leffler Mar 13 '18 at 07:00
-
1I don't get what C99 has to do with this. `&a[0]` is a constant expression in any version of the language. These are _address constants_, one valid form of a constant expression, as per C11 6.6 or C90 6.4. `gcc -std=c90 -pedantic` compiles the code just fine even if the variables are given static storage duration. What am I missing here? – Lundin Mar 13 '18 at 10:22
-
@Lundin : I would expect that `gcc -std=c90 -pedantic` would throw a warning, and `gcc -std=c99 -pedantic` to not throw a warning. Turbo-C considered it a hard error. Turbo-C acts more like GCC with the `-Werror` option in this case. – Michael Petch Mar 13 '18 at 12:48
-
@MichaelPetch But it doesn't. Why would any compiler throw a diagnostic? As far as I can tell, this is perfectly valid C and has always been perfectly valid C. – Lundin Mar 13 '18 at 12:54
-
My version of GCC (6.3) does throw a warning with `gcc -std=c90 -pedantic` when `a` is a local variable on the stack. If you move `a` to global scope or mark it `static` then you shouldn't get that warning because in that case the address of `a` is considered computable. at compile time. – Michael Petch Mar 13 '18 at 12:56
-
2Ah now I get it. This is what the standard actually says: "An _address constant_ is"... "... a pointer to an lvalue designating an object of **static storage duration**". This text is there from C90 to C11. And it makes perfect sense, since the variables in the question are allocated on the stack. But since C99 relaxed the requirement of initializers, an automatic storage variable does not need to be initialized with a constant expression. So @JonathanLeffler's answer is correct, it just isn't all that obvious why these initializers are not constant expressions. – Lundin Mar 13 '18 at 13:01
Believe it or not but the problem is actually your super ancient compiler. The code is fine. Just use a new compiler. Today most of the compilers are free and very small in size for downloading. So consider to upgrade to gcc or MSVC.

- 253
- 1
- 12
-
3I'm not sure about "very small size", but the rest of it is right — forget the old compiler and migrate to a newer one. – Jonathan Leffler Mar 13 '18 at 06:26
-
I meant gcc has small file size for downloading but I was not talking about MSVC's file size. I hope I can say the same about msvc in future, if microsoft decides to release a command line version of MSVC without the heavy IDE. – pro neon Mar 13 '18 at 06:32
-
I just took a look at my GCC installations (on a Mac). The biggest was GCC 6.2.0, weighing in at about 450 MiB; the smallest is GCC 7.3.0, weighing in at about 300 MiB. Those sizes are uncompressed, of course. The older versions included `gcj`, the GNU Java Compiler, and its deletion from later releases accounts for a lot of the shrinkage. Although they're not small, they're smaller than I expected. When building, you need about 4 GiB of free space in total for the source plus the multiple versions of the compiler that are built. I was expecting the installed size to be bigger. – Jonathan Leffler Mar 13 '18 at 06:42
-
While compiling it takes more space since object files are also produced and then they are linked. So might be that's the reason. – pro neon Mar 13 '18 at 06:48
-
Undoubtedly. I'm just a bit surprised that the overhead is 10:1 — I was expecting more like 4:1. – Jonathan Leffler Mar 13 '18 at 06:49
-
-
Why exactly is the compiler a problem? And how will upgrading to MSVC - a compiler with possibly even worse C conformance than Turbo C from 1989 help anyone? – Lundin Mar 13 '18 at 10:24
-
Well the TC compiler implements C90 standard which is really old and has some restrictions on such initialization. Hence the TC gives the error. Upgrading to a new compiler ensures that we use the latest standard which relaxes on these restrictions. I didn't implied that the user should only use MSVC. The user is free to use the compiler of their choice given that it uses latest standards. – pro neon Mar 13 '18 at 11:43
-
You should edit your answer and state the reasons why this doesn't work in C90. And you did imply that they should use a new compiler, which MSVC is not - it is about as old as Turbo C from 1989. Though as far as I know, Turbo C is better than Visual Studio at standard compliance. Getting MSVC is probably a downgrade. – Lundin Mar 13 '18 at 13:05
-
https://stackoverflow.com/users/584518/lundin According to wikipedia "Visual C++ 2017 (also known as Visual C++ 14.1) was released on March 7, 2017." So it supports C11 standard. If you don't believe me then just google MSVC. – pro neon Mar 13 '18 at 13:13
-
Visual C++ 2017, is a C++ compiler. MSVC refers to that one in C mode. The release date doesn't mean jack - you are incorrectly assuming that Microsoft cares about updating their compiler. It most definitely does not support C11. It only has partial support for C99. It has, IIRC, some conformance issues with C90. Since C90 is the only C standard it follows somewhat, Visual Studio 2017 in C mode, is a > 28 year old compiler, much like Turbo C. – Lundin Mar 13 '18 at 14:46