4

I am reading the book Let us C by Yashavant Kanetkar.

In the Array of Pointers section there is a section of code which is giving me problems:

int main()
{
    static int a[] = {0, 1, 2, 3, 4}; //-----------(MY PROBLEM)
    int *p[] = {a, a+1, a+2, a+3, a+4};
    printf("%u %u %d\n", p, *p, *(*p));
    return 0;
}

What I don't understand is: Why has the array a have to be initialized as static? I tried initializing it without the static keyword, but I got an error saying "illegal".

11
  • 4
    What is the error that you get? Commented Aug 30, 2012 at 6:31
  • Compiles and runs fine for me. Are you sure this is the source of your problems? Commented Aug 30, 2012 at 6:37
  • the above code runs...but if u remove the static keyword it doesnt...plz help... Commented Aug 30, 2012 at 6:43
  • @Yunus static means a is specified to have static storage, so treated as a pointer it is a compile-time constant expression. Array (as an aggregate type) initializers in C90 require constant expressions. Commented Aug 30, 2012 at 6:47
  • @veer bro i know what static means...what im not able to understand is why we have to use it here...i mean why do we have to initialize it as static??? plz dnt mind my narrowness... Commented Aug 30, 2012 at 6:49

2 Answers 2

6

C90 (6.5.7) had

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.

And you are initializing an object that has an aggregate type, so the value must be known at compile time and the address of automatic variables are not in that case.

Note this has changed in C99 (6.7.8/4)

All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

The constraint on object with aggregate or union type has been removed and I've not found it placed somewhere else. Your code with static removed should be accepted by a C99 compiler (it is by gcc -std=c99 for instance, which seems to confirm that I've not overlooked a constraint elsewhere).

Sign up to request clarification or add additional context in comments.

6 Comments

How does C99 6.7.8/7 explain this? Doesn't it tell the case when the object being initialized is static ?
a has static storage declaration, though, so a should be an address constant (and thus a constant expression) and a+1 etc. should be arithmetic constant expressions. This is accepted by C90 as well, AFAIK.
whoa!! so does that mean that to be proficient in C we have to know all these things in detail as well??
My understanding is that the OP is wondering why static is needed. It is needed in C90 but not in C99.
That is understood, but how the 6.7.8/8 helps explaining the thing. EDIT: Got it.
|
3

My guess would be that the contents of an array initialiser have to be a compile-time constant. By using static on a local variable in a function you essentially make that variable global, except with a local scope.

9 Comments

could you try elaborating please??
compiler is giving "illegal initialization" error...that too 5 errors
what do u mean by compile-time constant??
“compiler is giving "illegal initialization" error...that too 5 errors” — In C (and even more so C++) it's always advisable to ignore all errors but the first because they are very frequently just follow-up errors. A compile-time constant is an expression that can be fully evaluated at compile time. If a is static the compiler knows its address already (because it has to allocate space for it) so it's constant. If it's not static then it's a pointer to somewhere on the stack and thus not really constant.
@Joey but doesnt the compiler also know the address of any other variable for example an "int"....even for arrays it knows the base address right?? plz explain
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.