Array of problems with pointers

I tried this example pointer array. I get the error "Illegal initialization in main function"

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 anyone tell me the effect of static here. Many thanks.

+3
source share
4 answers

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

But this, apparently, has changed something in the standard, in the C90 he says:

, ,

, p , C99 :

, - .

+5

gcc gcc -ansi. gcc -ansi -pedantic :

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

8:

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

, , , a, b c p, . , , , . , " " , , . ( , / )

+4

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

, - :

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();
0

. . , . , , . (main). . , turbo-c. gcc : ( gcc -Wall prog.c

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

Source: https://habr.com/ru/post/1703225/


All Articles