gpt4 book ai didi

c - scanf 不会采用 &(*(p+i).grade)

转载 作者:行者123 更新时间:2023-11-30 16:37:47 26 4
gpt4 key购买 nike

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;
}

我不知道为什么scanf不需要&(*(p+i).grade) ?我知道*(p+i) ≡ p[i] ,但我不知道为什么它在这里不起作用。我对 *(p+i) 做了类似的事情在我写的另一个代码中:*p.age = 10;它成功地将值写入成员 age .

最佳答案

您正确地完成了从 a[b]*(a+b) 的转换,但您错过了点 的优先级。 运算符与解引用 * 运算符。

您需要强制评估运算符的方式,如下所示:

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

或使用->运算符,如下所示:

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

请注意使用 -> 运算符如何减少括号数量。

关于c - scanf 不会采用 &(*(p+i).grade),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47734192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com