如何为结构,结构内部的结构数组正确分配内存,并将该数组作为参数传递

时间:2022-09-06 11:56:38

I've been quite aways away from C and as I am diving back into it I have found myself hitting a roadblock. I have the following structure:

我一直远离C,当我潜入它时,我发现自己遇到了障碍。我有以下结构:

typedef struct{
      char id;
      struct S *children[SIZE];
}S;

In my code I initially declare an array of structs...

在我的代码中,我最初声明了一个结构数组......

 struct S arr[SIZE];

But when I get to this point of trying to allocate my first child for my first member of arr...

但是,当我到达这一点时,试图为我的第一个成员分配我的第一个孩子......

 arr[0].children[0] = (S*)malloc(sizeof(S));

I get this warning: warning: incompatible implicit declaration of built-in function ‘malloc’ warning: assignment from incompatible pointer type [enabled by default]

我收到此警告:警告:内置函数'malloc'警告的不兼容隐式声明:从不兼容的指针类型分配[默认启用]

On top of this I'm getting an error that doesn't sound very logical to me. I have the following function:

除此之外,我收到一个对我来说听起来不合逻辑的错误。我有以下功能:

int foo(S *children[SIZE]);

but when I call this line....

但是当我称这条线为......

foo(arr[0].children);

I get this note: note: expected ‘struct S **’ but argument is of type ‘struct S **’ which to me just sounds silly, it is expecting the argument it is getting and is upset about it.

我得到了这个注释:注意:预期'struct S **'但是参数类型为'struct S **',对我来说这听起来很愚蠢,它期待它得到的论点并且对此感到不安。

Any help in explaining what I should be doing to properly allocate this memory and achieve the same idea would be very much appreciated.

任何帮助解释我应该做什么来正确分配这个内存并实现相同的想法将非常感激。

2 个解决方案

#1


4  

There is no struct S, only S which is a typedef of anonymous structure.

没有struct S,只有S是匿名结构的typedef。

Define struct S too:

定义struct S:

typedef struct S {
  char id;
  struct S *children[SIZE];
}S;

Or:

typedef struct S S;
struct S {
  char id;
  S *children[SIZE];
};

And do avoid casting return of malloc in C:

并且避免在C中转换malloc的返回:

arr[0].children[0] = malloc(sizeof(S));

#2


1  

For your first problem, you need to do:

对于您的第一个问题,您需要:

#include <stdlib.h>

at the top of your program, in order to call malloc successfully.

在程序的顶部,以便成功调用malloc。

The second problem (as also pointed out by others) is that struct S in your class definition refers to a different struct than S. In C, struct tags are in a different "namespace" than type names.

第二个问题(也由其他人指出)是类定义中的struct S引用与S不同的结构。在C中,struct标签与类型名称在不同的“名称空间”中。

#1


4  

There is no struct S, only S which is a typedef of anonymous structure.

没有struct S,只有S是匿名结构的typedef。

Define struct S too:

定义struct S:

typedef struct S {
  char id;
  struct S *children[SIZE];
}S;

Or:

typedef struct S S;
struct S {
  char id;
  S *children[SIZE];
};

And do avoid casting return of malloc in C:

并且避免在C中转换malloc的返回:

arr[0].children[0] = malloc(sizeof(S));

#2


1  

For your first problem, you need to do:

对于您的第一个问题,您需要:

#include <stdlib.h>

at the top of your program, in order to call malloc successfully.

在程序的顶部,以便成功调用malloc。

The second problem (as also pointed out by others) is that struct S in your class definition refers to a different struct than S. In C, struct tags are in a different "namespace" than type names.

第二个问题(也由其他人指出)是类定义中的struct S引用与S不同的结构。在C中,struct标签与类型名称在不同的“名称空间”中。