c - 尝试访问动态指针数组时出现 SIGSEGV (C)

标签 c

当 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/

相关文章:

c - 我想打破存储在数组中 '\n' 字符处的字符串?

无法在我的 pop 函数中释放/取消分配空间?

c - C 中双指针和数组的不明确结果

c - C程序中的Shellcode

c - c中for循环的一个例子

c - 通过检查核心和调用堆栈了解使用 GDB 的 C 指针

c++ - 如何从给定日期找到星期几 `tm_wday`?

c - 结构错误的函数调用

c - sscanf : get first and last token in a string

无法从一组字符串中取出数字 - C