문제

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 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top