// PointerStruct.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #includeusing namespace std; // 结构体 struct stu{ char *name; //姓名 int num; //学号 int age; //年龄 char group; //所在小组 float score; //成绩 }; int _tmain(int argc, _TCHAR* argv[]) { struct stu stu1 = { "Tom", 12, 18, 'A', 136.5 }; //结构体指针 struct stu *pstu = &stu1; //读取结构体成员的值 //方法1 printf("%s的学号是%d,年龄是%d,在%c组,今年的成绩是%.1f!n", (*pstu).name, (*pstu).num, (*pstu).age, (*pstu).group, (*pstu).score); //方法2 printf("%s的学号是%d,年龄是%d,在%c组,今年的成绩是%.1f!n", pstu->name, pstu->num, pstu->age, pstu->group, pstu->score); getchar(); return 0; }
运行结果:



