简体   繁体   English

指向结构数组增量的指针失败

[英]pointer to struct array increment fails

struct book    *a =  malloc(2 *sizeof(struct book));
if (a == NULL)
{
    printf("Error\n");
    return 1;
}
struct book *p = a;

for(i=0; i<2; ++i){
    printf("Give the titel of no. %d book\n", i+1 );
    scanf("%s", *p->titel);

    ++p;
}

The code does not have any errors or warnings but once i input the titel of the 1st book and proceed to the 2nd it crushes. 该代码没有任何错误或警告,但是一旦我输入了第一本书的标题并进入第二本书,它就会崩溃。 What's wrong with the '++p'? “ ++ p”怎么了?

I suspect scanf("%s", *p->titel); 我怀疑scanf("%s", *p->titel); should have been written scanf("%s", p->titel); 应该已经写成scanf("%s", p->titel); instead, assuming book.titel is a char array. 相反,假设book.titel是一个char数组。 That could explain your observed behaviour. 那可以解释您观察到的行为。

Could you check? 你能检查一下吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM