2010-03-30 46 views
1

我想用一些基本的结构用C这样的:“解析错误”,在结构声明

struct p { 
    int a; 
    int b; 
    p * next; 
} 

但是,它未能与p * next;行与error: parse error before "p"编译。

你有什么想法可以解决这个问题吗?

回答

9

C的结构生活在一个不同的命名空间,必须要明确范围的,即:

struct p { 
    int a; 
    int b; 
    struct p * next; 
}; 

和结束时不要忘了分号! :-)

你可以假装你在C++:typedef struct p { /*...*/ } p;。但我认为next仍然会如上所述。

+1

你说得对,'struct p * next;'仍然需要typedef,因为typedef'd name * p *还不在范围内。 – 2010-03-30 08:02:55