编辑:从“struct Bar *”类型分配类型'struct Bar'时出现不兼容的类型

时间:2021-05-03 16:13:24

Edit: Code was cleaned up to avoid confuse on question. As far as the question: I want to allocate memory of size struct Foo2. Then allocate memory of size struct Bar and assign that location to a->b. Afterwards I want to set a->b.x to an integer value. This code is generated so I am looking to understand the concepts of allocating memory for structs and setting their values.

编辑:清理代码以避免混淆问题。至于问题:我想分配大小结构Foo2的内存。然后分配大小为struct Bar的内存并将该位置分配给a-> b。之后我想将a-> b.x设置为整数值。生成此代码,因此我希望了解为结构分配内存和设置其值的概念。

How do I access a->b? I grasp that I can't set a pointer to it.

我如何访问a-> b?我知道我无法设置它的指针。

Error:  incompatible types when assigning to type ‘struct Bar’ from
type ‘struct Bar *’?

Also why can I not put typedef struct Bar after the struct of Foo2 without an error: unknown type name ‘Bar’?

另外,为什么我不能在没有错误的Foo2结构之后放置typedef struct Bar:未知类型名称'Bar'?

typedef struct ForwardClassTest ForwardClassTest;
typedef struct Foo2 Foo2;
typedef struct Bar Bar;
struct ForwardClassTest {
};
struct Bar {
    int x;
};
struct Foo2 {
    Bar b;
};
int main();
  //Foo2 a
  Foo2 * a = (Foo2 *) malloc(sizeof(Foo2));
  //a=new Foo2()
  a = (Foo2*) malloc(sizeof(Foo2));
  //a.b=new Bar()
  a->b = (Bar*) malloc(sizeof(Bar));
  //a.b.x=5
  a->b.x = 5;
  exit(0);
}

2 个解决方案

#1


  1. This line Foo2 *a = (Foo2 *) malloc(sizeof(Foo2)) should be Foo2 * a = malloc(sizeof(Foo2))
  2. 这行Foo2 * a =(Foo2 *)malloc(sizeof(Foo2))应为Foo2 * a = malloc(sizeof(Foo2))

  3. The line a->b = (Bar*) malloc(sizeof(Bar)); is not required as b is not a pointer
  4. 线a-> b =(Bar *)malloc(sizeof(Bar));不是必需的,因为b不是指针

  5. The stuff:

    typedef struct ForwardClassTest ForwardClassTest; typedef struct Foo2 Foo2; typedef struct Bar Bar; typedef struct ForwardClassTest {

    typedef struct ForwardClassTest ForwardClassTest; typedef struct Foo2 Foo2; typedef struct Bar Bar; typedef struct ForwardClassTest {

is not required

不需要

  1. This is wrong void* main(char* args[]){ should be int main(int, char**) as the arguments are not used.
  2. 这是错误的void * main(char * args []){应该是int main(int,char **),因为没有使用参数。

  3. Use free to free the memory
  4. 使用free释放内存

#2


Since there was not a complete answer @ed-heal pointed me in right direction. I worked out solution. The only way to do this was by adding a pointer to Bar b in Foo2.

由于没有一个完整的答案@ ed-heal指出我正确的方向。我找到了解决方案。唯一的方法是在Foo2中添加一个指向Bar b的指针。

typedef struct Foo2 Foo2;
typedef struct Bar Bar;
struct Bar {
    int x;
};
struct Foo2 {
    Bar *b;
};
int main(){
  Foo2 * a = (Foo2 *) malloc(sizeof(Foo2));
  a = (Foo2*) malloc(sizeof(Foo2));
  a->b = (Bar*) malloc(sizeof(Bar));
  a->b->x = 5;
  free(a);
  free(a->b);
  exit(0);
}

#1


  1. This line Foo2 *a = (Foo2 *) malloc(sizeof(Foo2)) should be Foo2 * a = malloc(sizeof(Foo2))
  2. 这行Foo2 * a =(Foo2 *)malloc(sizeof(Foo2))应为Foo2 * a = malloc(sizeof(Foo2))

  3. The line a->b = (Bar*) malloc(sizeof(Bar)); is not required as b is not a pointer
  4. 线a-> b =(Bar *)malloc(sizeof(Bar));不是必需的,因为b不是指针

  5. The stuff:

    typedef struct ForwardClassTest ForwardClassTest; typedef struct Foo2 Foo2; typedef struct Bar Bar; typedef struct ForwardClassTest {

    typedef struct ForwardClassTest ForwardClassTest; typedef struct Foo2 Foo2; typedef struct Bar Bar; typedef struct ForwardClassTest {

is not required

不需要

  1. This is wrong void* main(char* args[]){ should be int main(int, char**) as the arguments are not used.
  2. 这是错误的void * main(char * args []){应该是int main(int,char **),因为没有使用参数。

  3. Use free to free the memory
  4. 使用free释放内存

#2


Since there was not a complete answer @ed-heal pointed me in right direction. I worked out solution. The only way to do this was by adding a pointer to Bar b in Foo2.

由于没有一个完整的答案@ ed-heal指出我正确的方向。我找到了解决方案。唯一的方法是在Foo2中添加一个指向Bar b的指针。

typedef struct Foo2 Foo2;
typedef struct Bar Bar;
struct Bar {
    int x;
};
struct Foo2 {
    Bar *b;
};
int main(){
  Foo2 * a = (Foo2 *) malloc(sizeof(Foo2));
  a = (Foo2*) malloc(sizeof(Foo2));
  a->b = (Bar*) malloc(sizeof(Bar));
  a->b->x = 5;
  free(a);
  free(a->b);
  exit(0);
}