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.
-
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– StoryTeller - Unslander Monica2018-03-13 06:15:18 +00:00Commented Mar 13, 2018 at 6:15
-
1You need some serious upgrade.haccks– haccks2018-03-13 06:15:29 +00:00Commented Mar 13, 2018 at 6:15
-
5That image made me nostalgic.taskinoor– taskinoor2018-03-13 06:17:33 +00:00Commented Mar 13, 2018 at 6:17
-
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– Jonathan Leffler2018-03-13 06:27:50 +00:00Commented Mar 13, 2018 at 6: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– Matteo Italia2018-03-13 06:36:52 +00:00Commented Mar 13, 2018 at 6:36
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.
7 Comments
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.&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?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.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.
11 Comments
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.