typedef 结构中的指针

Pointers in typedef structs

我有代码:

typedef struct foo *bar;

struct foo {
    int stuff
    char moreStuff;
}

为什么下面会出现incompatible pointer type错误?

foo Foo;
bar Bar = &Foo;

据我所知,bar 应该定义为指向 foo 的指针,不是吗?

完整的代码应该是这样的

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 以及 之后,尽管我认为这更像是一个 错别字].

像这样更改代码。

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;

应该是这样的:

typedef struct foo *bar;

struct foo {
    int stuff;
    char moreStuff;
};


int main()
{
  struct foo Foo;
  bar Bar = &Foo;

  return 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