【发布时间】:2021-03-14 18:11:01
【问题描述】:
我需要一个全局动态指针数组,我将在其中存储我的结构,因为稍后我需要遍历这个数组以列出所有存储的信息,我还需要能够读取@987654321 @、age 和 job 来自控制台的变量,并将它们存储在 person_t 的 iterator 数组中。
#include <stdio.h>
#include <stdlib.h>
typedef struct Person
{
char name[30];
int age;
char job[30];
} person_t;
person_t **iterator;
int capacity = 10;
int size = 0;
int main()
{
int i;
*iterator = (person_t *)malloc(capacity * sizeof(person_t));
for (i = 0; i < capacity; ++i)
{
person_t p;
p.age = i;
*iterator[i] = p;
}
return 0;
}
我在编译此代码 (gcc -ansi -pedantic -Wall -Wextra) 时没有收到任何错误/警告,但是当我尝试运行它时,我会立即收到 Segmentation fault。
【问题讨论】:
-
不需要投
malloc
标签: arrays c pointers struct malloc