typedef与struct

来源:互联网 发布:淘宝行业关键词 编辑:程序博客网 时间:2024/05/29 17:47

struct定义结构体的方式

第一种:


#include<stdio.h>int main(){struct {int a;} t;struct {int a;} t2;t.a=-1;t2.a=2;printf("%d%d\n",t.a,t2.a);return 0;}


使用struct { xxx; } t;直接定义结构体变量

第二种:

#include<stdio.h>int main(){struct node{int a;} t;struct node t2;t.a=-1;t2.a=2;printf("%d%d\n",t.a,t2.a);return 0;}


使用struct node {xxx} t;定义结构体node,同时定义个变量t

此后还可使用struct node这个标识符定义变量t2

typedef

typedef的使用语法为typedef 数据类型 标识符;

结合struct的用法一

#include<stdio.h>typedef struct{int a;}node;int main(){node t;node t2;t.a=-1;t2.a=2;printf("%d%d\n",t.a,t2.a);return 0;}



使用此种方式可以直接用标识符node定义变量

结合struct的用法二

#include<stdio.h>typedef struct NODE{int a;}node;int main(){node t;struct NODE t2;t.a=-1;t2.a=2;printf("%d%d\n",t.a,t2.a);return 0;}



可以使用标识符node或者struct NODE定义变量

以上代码gcc编译通过

0 0
原创粉丝点击