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
- This line
Foo2 *a = (Foo2 *) malloc(sizeof(Foo2))
should beFoo2 * a = malloc(sizeof(Foo2))
- The line
a->b = (Bar*) malloc(sizeof(Bar));
is not required asb
is not a pointer -
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 {
这行Foo2 * a =(Foo2 *)malloc(sizeof(Foo2))应为Foo2 * a = malloc(sizeof(Foo2))
线a-> b =(Bar *)malloc(sizeof(Bar));不是必需的,因为b不是指针
is not required
不需要
- This is wrong
void* main(char* args[]){
should beint main(int, char**)
as the arguments are not used. - Use
free
to free the memory
这是错误的void * main(char * args []){应该是int main(int,char **),因为没有使用参数。
使用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
- This line
Foo2 *a = (Foo2 *) malloc(sizeof(Foo2))
should beFoo2 * a = malloc(sizeof(Foo2))
- The line
a->b = (Bar*) malloc(sizeof(Bar));
is not required asb
is not a pointer -
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 {
这行Foo2 * a =(Foo2 *)malloc(sizeof(Foo2))应为Foo2 * a = malloc(sizeof(Foo2))
线a-> b =(Bar *)malloc(sizeof(Bar));不是必需的,因为b不是指针
is not required
不需要
- This is wrong
void* main(char* args[]){
should beint main(int, char**)
as the arguments are not used. - Use
free
to free the memory
这是错误的void * main(char * args []){应该是int main(int,char **),因为没有使用参数。
使用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);
}