c语言关于指针引用的问题

来源:百度知道 编辑:UC知道 时间:2024/07/01 07:17:54
定义 struct student {int age ;int num;} std,*p=&std;则对age引用不正确的是()
A.*p.age B.p->age C.* std.age D.(*p).age
答案我知道了,是选A。希望大侠帮我分析下每个答案的错对原因,谢谢咯!

p指向std的地址
A.*p.age等效于*(p.age),p.age这种格式已经不通过,错
B.p->age,是标准用法,对
C.std.age是int型,不能再*附,错
D.(*p).age等价于std.age,对

你可以尝试编译
#include <stdio.h>
struct student {int age ;int num;} std,*p=&std;
main()
{
*p.age; //error C2228: left of '.age' must have class/struct/union type
p->age;
*std.age; //error C2100: illegal indirection
(*p).age;
}
只有BD项成功通过编译

struct student 定义的一个结构体, 而std是一个结构体变量,他所在内存中映射的就是struct student这样一种结构,*p = &std; 是将std结构体变量的首地址赋值给p,那么p就指向了这个结构体的首地址,所以通过指针来引用结构体中的成员变量,没有A这种写法..