我在结构中有一个结构和一个动态数组。我想 malloc 这个数组,但我现在真的不知道怎么做。我希望该数组无效,因为我希望该数组的成员是结构。如您所见,我尝试了一些方法,但它并没有真正起作用
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct saf
{
int head;
void **stack;
int size;
}exp1;
void init(int n)
{
struct saf exp1->stack = malloc(n);
}
int main()
{
printf("Give size: ");
scanf("%d",&exp1.size);
init(exp1.size);
return 0;
}
最佳答案
exp1 不是指针。使用
exp1.stack = malloc(n);
关于c - 结构的 Malloc 成员数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22623413/