我有代码:
typedef struct foo *bar;
struct foo {
int stuff
char moreStuff;
}
为什么以下内容会出现incompatible pointer type
错误?
foo Foo;
bar Bar = &Foo;
据我所知,bar
应该被定义为指向foo
的指针,不是吗?
答案 0 :(得分:2)
完整的代码应该是
typedef struct foo *bar;
typedef struct foo { //notice the change
int stuff;
char moreStuff;
}foo;
和用法
foo Foo;
bar Bar = &Foo;
如果struct foo
中没有typedef,则代码不会编译。
另外,请注意结构定义之后的;
[以及int stuff
之后,尽管我认为错误 >]。
答案 1 :(得分:1)
应该是这样的:
typedef struct foo *bar;
struct foo {
int stuff;
char moreStuff;
};
int main()
{
struct foo Foo;
bar Bar = &Foo;
return 0;
}
答案 2 :(得分:0)
像这样更改代码。
typedef struct foo *bar;
struct foo {
int stuff;
char moreStuff;
};
struct foo Foo;
bar Bar = &Foo;
否则你可以将typedef用于该结构。
typedef struct foo {
int stuff;
char moreStuff;
}foo;
foo Foo;
bar Bar=&Foo;
答案 3 :(得分:0)
this code is very obfuscated/ cluttered with unnecessary, undesirable elements.
In all cases, code should be written to be clear to the human reader.
this includes:
-- not making instances of objects by just changing the capitalization.
-- not renaming objects for no purpose
suggest:
struct foo
{
int stuff;
char moreStuff;
};
struct foo myFoo;
struct foo * pMyFoo = &myFoo;
which, amongst other things, actually compiles