开发者

"Parse error" in struct declaration

开发者 https://www.devze.com 2022-12-25 05:31 出处:网络
I want to use some basic struct in C like the following: struct p { int a; int b; p * next; } However, it fails to compile with an error: parse error before \"p\" on the line开发者_Go百科 with p *

I want to use some basic struct in C like the following:

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

However, it fails to compile with an error: parse error before "p" on the line开发者_Go百科 with p * next;.

Do you have any idea what the reason could be for this problem?


C structs live in a different namespace and have to be explicitly scoped, thus:

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

And don't forget the semicolon at the end! :-)

You can pretend you're in C++ thus: typedef struct p { /*...*/ } p;. But I think that next will still have be declared as above.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号