写结构体指针前,先说一下 . 号和 -> 的区别 记得当初刚学C语言的时候,搞不清结构体的 . 号和 -> ,经常混淆二者的使用。 那么在C语言中 . 号是成员访问运算符,当我们需要访问结构的成员的时候,就会使用到它 而当我们需要使用结构体指针来访问结构成员的时候,就得使用->运算符了。 结构体指针 结构体指针,就是指向结构体的指针。 #include<stdio.h> #include<string.h> typedef struct student{ int id; char name[10]; char sex; }stu; //结构体别名 void PrintStu(stu *student); int main() { stu stu1; printf("sizeof of stu1 is:%d\n",sizeof(stu1)); stu1.id=2014; strcpy(stu1.name,"zhangfei"); stu1.sex='m'; PrintStu(&stu1); return 0; } void PrintStu(stu *student) { printf("stu1 id is :%d\n",student->id); printf("stu1 name is :%s\n",student->name); printf("stu1 sex is :%c\n",student->sex); }
|
|