简体   繁体   中英

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'?

I suspect scanf("%s", *p->titel); should have been written scanf("%s", p->titel); instead, assuming book.titel is a char array. That could explain your observed behaviour.

Could you check?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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