使用(.)
声明一个结构体和结构体变量。用结构体名+“.”+成员就可以引用域了。因为已经分配了该结构体变量的内存。如同int a;一样。
使用->
声明一个结构体、结构体变量和结构体指针,在已经开辟了该结构体变量内存的情况下,把该结构体指针指向该结构体的内存位置。
示例:
int main()
{
struct student //声明定义一个结构体
{
char name[20];
int year;
};
struct student student1; //结构体变量student1
struct student *student1_point; //结构体指针student_point
student1_point=&student1; //结构体指针指向了结构体变量
strcpy(student1.name,"liming");
student1.year=19;
//使用(.)访问成员
printf("struct:%s is %d years old.\n",student1.name,student1.year);
//使用->访问成员
printf("point:%s is %dyearsold.\n",student1_point->name,student1_point->year);
return 0;
}
输出:
struct:liming is 19 years old.
point:liming is 19years old.