天天看点

浅谈c语言typedef 与结构体指针(个人小经验)



#include<stdio.h>

#include<string.h>

typedef struct emp{

 char sex[8];

 char name[15];

 int age;

}*emp;//这里我们用typedef把emp这个结构体变成了*emp这种指向结构体成员的结构体指针

/*typedef struct emp{

}pi,*emp;//为了程序的可读性最好不要这样声明*/

int main(){

 emp p;//注意这里emp是指向结构体的指针变量 emp *p这样的声明是错误的,emp p这样声明了结构体类型的指针变量p

 strcpy(p->sex,"male");

 strcpy(p->name,"zhangfei");

 p->age=20;

 printf("sex is %s\n",p->sex);

 printf("name is %s\n",p->name);

 printf("age is %d\n",p->age);

 return 0;

}