成都高端品牌网站建设,惠州seo排名,深圳影视广告哪里有提供,wordpress阅读量的统计0 前言
在进行嵌入式开发的过程中#xff0c;我们经常会见到typedef这个关键字#xff0c;这个关键字的作用是给现有的类型取别名#xff0c;在实际使用过程中往往是将一个复杂的类型名取一个简单的名字#xff0c;便于我们的使用。就像我们给很熟的人取外号一样#xff…0 前言
在进行嵌入式开发的过程中我们经常会见到typedef这个关键字这个关键字的作用是给现有的类型取别名在实际使用过程中往往是将一个复杂的类型名取一个简单的名字便于我们的使用。就像我们给很熟的人取外号一样就是便于我们的记忆和使用。
1 给结构体取别名的4种方法
1.1 定义结构体时取别名1
示例代码
typedef struct
{int x;int y;
} test1_t;结果给结构体取别名为test1_t。 这种方法最简洁但存在一个问题如果我们定义的结构体是一个链表则无法定义一个本身结构体类型的指针。
1.2 定义结构体时取别名2
示例代码
typedef struct t2
{int x;int y;
} test2_t;结果给结构体取别名为test2_t。 这种方法比上面那种略复杂但可以定义一个本身结构体类型的指针。在结构体内定义本身结构体类型的指针的语句struct t2 *x;
1.3 定义结构体完成后取别名1
struct t3
{int x;int y;
};
typedef struct t3 test3_t;结果给结构体struct t3取别名为test2_t。 这种方法写得更加臃肿一些不过结构看起来更加明朗。
1.4 定义结构体完成后取别名2
struct t3
{int x;int y;
};
typedef struct t3 *test4_t;结果给struct t3取别名为*test4_t相当于test4_t是struct t3型指针类型。 这种方法可以直接给结构体取一个指针别名类型这样取别名后定义一个结构体指针变量语句为test4_t x;
2 结果测试
将上述的操作方法放到一个源文件内设置并打印结构体成员的值。测试代码如下
#include stdio.htypedef struct
{int x;int y;
} test1_t;typedef struct t2
{int x;int y;
} test2_t;struct t3
{int x;int y;
};
typedef struct t3 test3_t;
typedef struct t3 *test4_t;int main(void)
{test1_t test1;test2_t test2;test3_t test3;test4_t test4 test3;test1.x 1;test1.y 2;test2.x 1;test2.y 2;test3.x 1;test3.y 2;printf(test 1 x : %d, y : %d\r\n, test1.x, test1.y);printf(test 2 x : %d, y : %d\r\n, test2.x, test2.y);printf(test 3 x : %d, y : %d\r\n, test3.x, test3.y);printf(test 4 x : %d, y : %d\r\n, test4-x, test4-y);return 0;
}测试结果