function pointers

when i try assign a pointer globally why does the compiler gives me error?
it works fine when i do this inside the main function.

1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>

int x=10,*p;

p=&x;

int main()
{
    printf("%d",*p);
    return 0;
}
Last edited on
Only declarations (and initializations) and definitions are allowed in global scope. Operations must go into functions.
Thanks
Does this work?
1
2
3
4
5
6
7
8
9
#include<stdio.h>

int x=10,*p = &x;

int main()
{
    printf("%d",*p);
    return 0;
}
Topic archived. No new replies allowed.