博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C中的结构化数据类型-Struct和Typedef用示例解释
阅读量:2519 次
发布时间:2019-05-11

本文共 3090 字,大约阅读时间需要 10 分钟。

During your programming experience you may feel the need to define your own type of data. In C this is done using two keywords: struct and typedef. Structures and unions will give you the chance to store non-homogenous data types into a single collection.

在进行编程的过程中,您可能会需要定义自己的数据类型。 在C语言中,这是使用两个关键字完成的: structtypedef 。 结构和联合将使您有机会将非均匀数据类型存储到单个集合中。

声明新的数据类型 (Declaring a new data type)

typedef struct student_structure{    char* name;    char* surname;    int year_of_birth;}student;

After this little code student will be a new reserved keyword and you will be able to create variables of type student. Please mind that this new kind of variable is going to be structured which means that defines a physically grouped list of variables to be placed under one name in a block of memory.

在这个小代码之后, student将成为新的保留关键字,您将能够创建类型为student变量。 请注意,这种新型变量将被结构化,这意味着将定义一组物理分组的变量列表,这些变量列表将以一个名称放置在内存块中。

新数据类型的用法 (New data type usage)

Let’s now create a new student variable and initialize its attributes:

现在让我们创建一个新的student变量并初始化其属性:

student stu;    strcpy( stu.name, "John");   strcpy( stu.surname, "Snow");    stu.year_of_birth = 1990;    printf( "Student name : %s\n", stu.name);   printf( "Student surname : %s\n", stu.surname);   printf( "Student year of birth : %d\n", stu.year_of_birth);

As you can see in this example you are required to assign a value to all variables contained in your new data type. To access a structure variable you can use the point like in stu.name. There is also a shorter way to assign values to a structure:

如本例所示,您需要为新数据类型中包含的所有变量分配一个值。 要访问结构变量,可以使用stu.name的点。 还有一种将值分配给结构的较短方法:

typedef struct{   int    x;   int    y;}point;point image_dimension = {640,480};

Or if you prefer to set it’s values following a different order:

或者,如果您希望按照其他顺序设置其值:

point img_dim = { .y = 480, .x = 640 };

联合与结构 (Unions vs Structures)

Unions are declared in the same was as structs, but are different because only one item within the union can be used at any time.

联合的声明与结构相同,但是有所不同,因为联合中的任何一项都可以在任何时候使用。

typedef union{      int circle;      int triangle;      int ovel;}shape;

You should use union in such case where only one condition will be applied and only one variable will be used. Please do not forget that we can use our brand new data type too:

在仅应用一个条件且仅使用一个变量的情况下,应使用union 。 请不要忘记我们也可以使用全新的数据类型:

typedef struct{      char* model;      int year;}car_type;typedef struct{      char* owner;      int weight;}truck_type;typedef union{  car_type car;  truck_type truck;}vehicle;

一些技巧 (A few more tricks)

  • When you create a pointer to a structure using the & operator you can use the special -> infix operator to deference it. This is very used for example when working with linked lists in C

    当使用&运算符创建指向结构的指针时,可以使用特殊的-> infix运算符来引用它。 例如,当在C中使用链接列表时,这非常有用

  • The new defined type can be used just as other basic types for almost everything. Try for example to create an array of type student and see how it works.

    新定义的类型可以像几乎所有其他基本类型一样使用。 例如,尝试创建一个类型为student的数组,并查看其工作方式。

  • Structs can be copied or assigned but you can not compare them!

    可以复制或分配结构,但不能进行比较!

更多信息: (More Information:)

翻译自:

转载地址:http://imrwd.baihongyu.com/

你可能感兴趣的文章
HDU 1829/POJ 2492 A Bug's Life
查看>>
CKplayer:视频推荐和分享插件设置
查看>>
CentOS系统将UTC时间修改为CST时间
查看>>
redis常见面试题
查看>>
导航控制器的出栈
查看>>
玩转CSS3,嗨翻WEB前端,CSS3伪类元素详解/深入浅出[原创][5+3时代]
查看>>
iOS 9音频应用播放音频之播放控制暂停停止前进后退的设置
查看>>
Delphi消息小记
查看>>
HNOI2016
查看>>
JVM介绍
查看>>
将PHP数组输出为HTML表格
查看>>
Java中的线程Thread方法之---suspend()和resume() 分类: ...
查看>>
经典排序算法回顾:选择排序,快速排序
查看>>
BZOJ2213 [Poi2011]Difference 【乱搞】
查看>>
一道关于员工与部门查询的SQL笔试题
查看>>
Canvas基础
查看>>
[Hive - LanguageManual] Alter Table/Partition/Column
查看>>
可持久化数组
查看>>
去除IDEA报黄色/灰色的重复代码的下划波浪线
查看>>
Linux发送qq、网易邮件服务配置
查看>>