I am trying to use malloc to allocate heap memory for an array of struct pointers, but I cannot get it to work. Below is my code, but when I compile with gcc, I got errors like this "error: invalid type argument of ‘->’ "
我试图使用malloc为一个struct指针数组分配堆内存,但我无法让它工作。下面是我的代码,但是当我使用gcc编译时,我得到了类似的错误“错误:' - >'的无效类型参数”
The array I want to set up an array of mystruct_pointer, which should point to the actual __mystruct_t, and I think I can use "->" on its member field. Where is wrong with my code? I think it should work. thanks
我要设置一个mystruct_pointer数组的数组,它应该指向实际的__mystruct_t,我想我可以在其成员字段上使用“ - >”。我的代码出了什么问题?我认为它应该工作。谢谢
typedef struct
{
int id;
bool status;
} __mystruct_t;
typedef __mystruct_t* mystruct_pointer;
mystruct_pointer struct_ptr_array;
void my_init(int number)
{
struct_ptr_array = (mystruct_pointer) malloc(sizeof(__mystruct_t) * number);
int i;
for (i = 0; i < number; i++) /* initialize the array of struct pointers */
{
struct_ptr_array[i]->id = i;
struct_ptr_array[i]->status = false;
}
}
4 个解决方案
#1
4
Replace '->' by '.'. Since 'struct_ptr_array[i]' already dereference the pointer.
将' - >'替换为'。'。因为'struct_ptr_array [i]'已经取消引用指针。
#2
2
Problems like these come from doing obscure things. You typedef-hide a pointer and instantly you have made the program unreadable to yourself. And that's the only thing the typedef achieved. So never hide pointers with typedefs, it is very bad practice.
像这样的问题来自做些模糊不清的事情。你输入隐藏指针,你立即让你自己的程序不可读。而这是typedef唯一实现的目标。所以永远不要用typedef隐藏指针,这是非常糟糕的做法。
Other issues:
其他事宜:
-
Avoid double underscore because that's reserved for compiler identifiers.
避免使用双下划线,因为它是为编译器标识符保留的。
-
Don't cast the result of malloc, because doing so is completely pointless in C and also potentially dangerous on old C compilers.
不要转换malloc的结果,因为这样做在C中完全没有意义,并且在旧的C编译器上也有潜在危险。
-
Handle the case where malloc fails because there's no heap space available.
处理malloc失败的情况,因为没有可用的堆空间。
-
Never use variables with global scope, it is very bad practice that leads to spaghetti code. And there is never a reason to do so in C.
永远不要使用具有全局范围的变量,这是导致意大利面条代码的非常糟糕的做法。并且没有理由在C中这样做。
The code should be fixed as follows:
代码应修复如下:
typedef struct
{
int id;
bool status;
} mystruct_t;
static mystruct_t* struct_ptr_array;
void my_init(int number)
{
struct_ptr_array = malloc(sizeof(mystruct_t) * number);
if(struct_ptr_array == NULL)
{
handle_error();
return ;
}
for (int i = 0; i < number; i++) /* initialize the array of struct pointers */
{
struct_ptr_array[i].id = i;
struct_ptr_array[i].status = false;
}
}
#3
1
struct_ptr_array
is a pointer, but you index it, so you get an actual __mystruct_t
, not the pointer. Thus, simply use:
struct_ptr_array是一个指针,但是你将它编入索引,所以你得到一个实际的__mystruct_t,而不是指针。因此,只需使用:
struct_ptr_array[i].id = i;
struct_ptr_array[i].status = false;
#4
1
Replace two lines by
将两行替换为
struct_ptr_array[i].id = i;
struct_ptr_array[i].status = false;
#1
4
Replace '->' by '.'. Since 'struct_ptr_array[i]' already dereference the pointer.
将' - >'替换为'。'。因为'struct_ptr_array [i]'已经取消引用指针。
#2
2
Problems like these come from doing obscure things. You typedef-hide a pointer and instantly you have made the program unreadable to yourself. And that's the only thing the typedef achieved. So never hide pointers with typedefs, it is very bad practice.
像这样的问题来自做些模糊不清的事情。你输入隐藏指针,你立即让你自己的程序不可读。而这是typedef唯一实现的目标。所以永远不要用typedef隐藏指针,这是非常糟糕的做法。
Other issues:
其他事宜:
-
Avoid double underscore because that's reserved for compiler identifiers.
避免使用双下划线,因为它是为编译器标识符保留的。
-
Don't cast the result of malloc, because doing so is completely pointless in C and also potentially dangerous on old C compilers.
不要转换malloc的结果,因为这样做在C中完全没有意义,并且在旧的C编译器上也有潜在危险。
-
Handle the case where malloc fails because there's no heap space available.
处理malloc失败的情况,因为没有可用的堆空间。
-
Never use variables with global scope, it is very bad practice that leads to spaghetti code. And there is never a reason to do so in C.
永远不要使用具有全局范围的变量,这是导致意大利面条代码的非常糟糕的做法。并且没有理由在C中这样做。
The code should be fixed as follows:
代码应修复如下:
typedef struct
{
int id;
bool status;
} mystruct_t;
static mystruct_t* struct_ptr_array;
void my_init(int number)
{
struct_ptr_array = malloc(sizeof(mystruct_t) * number);
if(struct_ptr_array == NULL)
{
handle_error();
return ;
}
for (int i = 0; i < number; i++) /* initialize the array of struct pointers */
{
struct_ptr_array[i].id = i;
struct_ptr_array[i].status = false;
}
}
#3
1
struct_ptr_array
is a pointer, but you index it, so you get an actual __mystruct_t
, not the pointer. Thus, simply use:
struct_ptr_array是一个指针,但是你将它编入索引,所以你得到一个实际的__mystruct_t,而不是指针。因此,只需使用:
struct_ptr_array[i].id = i;
struct_ptr_array[i].status = false;
#4
1
Replace two lines by
将两行替换为
struct_ptr_array[i].id = i;
struct_ptr_array[i].status = false;