gpt4 book ai didi

c - c 中 malloc 与许多结构的用法

转载 作者:行者123 更新时间:2023-11-30 19:55:14 24 4
gpt4 key购买 nike

我尝试用我的结构存储很多东西。这些变量似乎存储在“do-while”中,但是当我尝试访问在 do-while 之后存储的元素时,该元素消失了。我做错了什么?

#include <stdio.h>
#include <stdlib.h>

struct Sauto {
int part;

};

int main() {
int i=0;
struct Sauto *car;

do {
car = malloc(sizeof(struct Sauto)+(sizeof(struct Sauto)*i));
struct Sauto car[i];

printf("part%i:", i);
scanf("%i", &car[i].part);
printf("part%i is %i\n", i, car[i].part);
i++;
} while (i<3);
printf("part 0 is %i\n", car[0].part);
return 0;
}

最佳答案

您重新声明了一个同名“car”的变量。删除do-while循环中的声明。

关于c - c 中 malloc 与许多结构的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35538300/

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