0
void storing(struct student * p, int count)
{
    int i,j;

    for ( i = 0; i < count; i++)
    {
        printf("the %d student's info\n", i+1);

        printf("name:\n");
        scanf("%s", p[i].name); 
        printf("age:\n");
        scanf("%d", &((&p[i])->age));  
        printf("grade:\n");
        scanf("%d", &(*(p+i).grade));        
    }
    return;
}

I don't know why scanf doesn't take &(*(p+i).grade)? I know *(p+i) ≡ p[i], but I don't know why it just doesn't work here. I did similar thing with *(p+i) in another code I written: *p.age = 10; and it successfully writes the value into member age.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Yu Jia
  • 21
  • 5

1 Answers1

0

You did the conversion from a[b] to *(a+b) correctly, but you missed the precedence of the dot . operator vs. the dereference * operator.

You need to force the way the operators are evaluated, like this:

scanf("%d", &((*(p+i)).grade)); // Add parentheses and keep dot . operator

or use -> operator, like this:

scanf("%d", &(p+i)->grade); // Replace dot . with -> operator

Note how the use of -> operator lets you reduce the number of parentheses.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523