gpt4 book ai didi

c - 结构的 Malloc 成员数组

转载 作者:太空宇宙 更新时间:2023-11-04 06:29:13 25 4
gpt4 key购买 nike

我在结构中有一个结构和一个动态数组。我想 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com