Reputation: 1369
Why do I get error in this code?Even if i do not link but only compile still it gives an error.why does not compiler consider the possibility of it being present in another file? Could anyone explain how typedef statement is treated by compiler.
Thanks in advance
typedef struct p* q;
int main()
{
struct p{
int x;
char y;
q ptr;
};
struct p p={1,2,&p};
printf("%d\n",p.ptr->ptr->x);
return 0;
}
ps:dereference to incomplete type is the error i get in gcc.
Upvotes: 0
Views: 94
Reputation: 222933
The “struct p” outside main and the “struct p” inside main are different types because they are defined in different scopes. If you put both declarations outside main or both inside main, the compiler will accept it.
When you define something inside a function, you are saying “Here is a definition I am using just inside this function; it is not applicable to anything outside the function.”
Upvotes: 2