C中的结构化数据类型-Struct和Typedef用示例解释

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:)

翻译自: https://www.freecodecamp.org/news/structured-data-types-in-c-struct-and-typedef-explained-with-examples/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值