0

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.

enter image description here

14
  • 5
    It'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. Commented Mar 13, 2018 at 6:15
  • 1
    You need some serious upgrade.
    – haccks
    Commented Mar 13, 2018 at 6:15
  • 5
    That image made me nostalgic.
    – taskinoor
    Commented Mar 13, 2018 at 6:17
  • 3
    Most 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). Commented 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... 🤔 Commented Mar 13, 2018 at 6:36

2 Answers 2

5

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
  • 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. Commented Mar 13, 2018 at 6:59
  • Agreed! Commented Mar 13, 2018 at 7:00
  • 1
    I 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
    Commented Mar 13, 2018 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. Commented Mar 13, 2018 at 12:48
  • 2
    Ah 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
    Commented Mar 13, 2018 at 13:01
3

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
  • 3
    I'm not sure about "very small size", but the rest of it is right — forget the old compiler and migrate to a newer one. Commented Mar 13, 2018 at 6: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
    Commented Mar 13, 2018 at 6: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. Commented Mar 13, 2018 at 6: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
    Commented Mar 13, 2018 at 6:48
  • Undoubtedly. I'm just a bit surprised that the overhead is 10:1 — I was expecting more like 4:1. Commented Mar 13, 2018 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.