Error request for member in something not a structure or union(指针类型的转换)

来源:互联网 发布:linux alias 配置文件 编辑:程序博客网 时间:2024/06/09 07:37
运行环境,PC 64位,编译工具:Dec-C++

上个例子就清楚了。

#include <stdio.h>typedef struct {int num;char name[10];}Stu,*ptrStu;int main(void){Stu Tom = {10,"Tom"};char *p = &Tom;printf("num=%d,name=%s\n",p->num,p->name);//编译时出错,要对指针进行显式的类型转换 printf("num=%d,name=%s\n",(ptrStu)p->num,(ptrStu)p->name);/*[Error] request for member 'num' in something not a structure or union。()和->优先级相同,且从左到右结合。此处,不知为何出错,保险起见,加一个().*/ printf("num=%d,name=%s\n",((ptrStu)p)->num,((ptrStu)p)->name);//okreturn 0;}



0 0