#include<stdio.h>
struct data{
int i;
struct data *p;
};
int main() {
struct data *p=malloc(sizeof(struct data));
//How do i use pointer to structure to read a integer in member variable i?
scanf("%d",&p->i); // I am advised to use this,Can you interpret this??
scanf("%d",&(*p).i); // Is this valid?
scanf("%d",p->i); // Why is this not valid since p is nothing but a pointer
}
解释此
&p->i
。为什么这代表成员变量i?
的地址这
scanf("%d",&(*p).i);
有效吗?为什么?
在您的情况下
-
&p->i
与&(p->i)
相同,因为操作员优先。 -
&(*p).i
与&(p->i)
相同。
,它们都按照scanf()
函数参数基于提供的转换说明符的要求。
但是,
scanf("%d",p->i);
无效,因为p->i
为您提供了int
,而您需要一个指针到整数。
scanf
期望某物指针,以便根据您提供给函数的格式 store 数据。
scanf("%d",&p->i); // I am advised to use this,Can you interpret this??
p->i
从指向p
指向的结构中为您提供整数i
。
&p->i
给出了i
的地址,Scanf。
scanf("%d",&(*p).i); //Is this valid?
是的,这与上面相同。(*p).i
是p->i
scanf("%d",p->i); //Why is this not valid since p is nothing but a pointer
scanf
需要一个指针来存储"%d",这意味着整数;但是,在这里您给出了i
的值,而不是i
的指针。