3

I have tried this example of array of pointers. I am getting the error "Illegal initialisation in function main"

int main()
{
    int a[]={1,2,3,4,5};
    int b[]={1,2,3,4,5};
    int c[]={1,2,3,4,5};
    int *p[3]={a,b,c};
    int i;
    clrscr();
    for(i=0;i<3;i++)
        printf("%d - %u\n",*p[i],p[i]);
    getch();
}

If I use static int instead of int in array declarations, it works fine. Can any one tell me the effect of static here. Thanks a lot.

6
  • What line is producing this error and which compiler are you using? VS2008 builds this clean Commented Feb 14, 2009 at 18:39
  • Compiles correctly with no errors in gcc; what header files are you including? Commented Feb 14, 2009 at 18:46
  • The first 3 array declarations are producing the error. I am using turbo c. Commented Feb 14, 2009 at 18:48
  • I am including stdio and conio header files Commented Feb 14, 2009 at 18:49
  • when I see the help, it says that initialisations must be the address of a static or global variable or constant expressions. Commented Feb 14, 2009 at 18:51

4 Answers 4

5

In gcc you see warnings about this if you use the -pedantic flag.

But this is apparently something that has changed in the standard, in C90 it say:

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 it was not allowed as the p array is an aggregate type, but in C99 we have:

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

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

1 Comment

The rule you cite doesn't apply here. p doesn't have static storage duration.
4

This compiles fine with gcc and gcc -ansi. gcc -ansi -pedantic however gives the following warning:

blackjack.c: In function ‘main’:
blackjack.c:8: warning: initializer element is not computable at load time
blackjack.c:8: warning: initializer element is not computable at load time
blackjack.c:8: warning: initializer element is not computable at load time

Whereas line 8 is:

int *p[3]={a,b,c};

As I see it the problem is that at the time a, b, and c would be stored in p, they don't exist yet. That's because they will be put on the stack and the position on the stack depends on things outside the scope of a function. To clarify this, 'load time' means the time the program is loaded into memory, not the time it is already in execution. (Don't ask me why/how it works anyway)

Comments

0

try: printf("%d - %u\n",*(p[i]),p[i]);

although I have a feeling you're trying to do something more like:

int a[]={1,2,3,4,5};
int b[]={1,2,3,4,5};
int c[]={1,2,3,4,5};
int *p[3]={a,b,c};
int i;
clrscr();
for(i=0;i<sizeof(p)/sizeof(int*);i++) {
    for (int j =0; j < sizeof(a)/sizeof(int); j++) {
        printf("%d - %u\n",(p[i])[j],p[i]);
    }
}
getch();

Comments

0

The rules are pretty simple.For static objects the initialization list should be constant.No such restriction exists for the elements that will be allocated space on stack. It appears logical too,as the static objects need to be written in the data section and the compiler must be able to resolve their values beforehand. On stack memory is allocated once the function in question(main) is called.So no problem there.I don't know why the opposite behavior on turbo-c. on gcc this happens:(on compiling with gcc -Wall prog.c

        int *p[]={a,b,c} //works fine
        static int *p[]={a,b,c} //oops blunder

Comments

Your Answer

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