当 scanf 尝试访问 titolo[i]->nome 时出现错误,我不明白为什么
typedef struct tit *TITOLO;
struct tit {
char nome[20];
};
int main()
{
TITOLO *titolo;
titolo =(TITOLO *) malloc(4*sizeof (TITOLO));
if (titolo == NULL) exit(1);
int i;
for (i=0;i<4;i++) {
printf("Insert title: ");
scanf("%s", titolo[i]->nome);
}
return 0;
}
最佳答案
typedef struct tit *TITOLO;
将 TITOLO 定义为指针类型,而不是结构类型。摆脱它并改为对结构进行类型定义:
typedef struct {
char nome[20];
} TITOLO;
TITOLO* titolo = malloc(4*sizeof(*titolo));
关于c - 尝试访问动态指针数组时出现 SIGSEGV (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54292411/