💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# C 编程中的结构 > 原文: [https://beginnersbook.com/2014/01/c-structures-examples/](https://beginnersbook.com/2014/01/c-structures-examples/) **结构**是由单个名称表示的不同数据类型的一组变量。让我们举一个例子来理解 C 编程中结构的需要。 让我们说我们需要存储学生的数据,如学生姓名,年龄,地址,身份证等。这样做的一种方法是为每个属性创建一个不同的变量,但是当你需要存储多个学生的数据时,那么例如,您需要为每个学生再次创建这几个变量。以这种方式存储数据非常令人头疼。 我们可以通过使用结构轻松解决这个问题。我们可以创建一个具有`name`,`id`,`address`和`age`成员的结构,然后我们可以为每个学生创建这个结构的变量。这可能听起来令人困惑,不要担心我们会在示例的帮助下理解这一点。 ## 如何在 C 编程中创建一个结构 我们使用 **`struct`** 关键字在 C 中创建结构。`struct`关键字是结构化数据类型的简短形式。 ```c struct struct_name { DataType member1_name; DataType member2_name; DataType member3_name; … }; ``` 这里`struct_name`可以是你选择的任何东西。成员数据类型可以相同或不同。一旦我们声明了结构,我们就可以使用`struct name`作为数据类型,如`int`,`float`等。 > 首先,我们将看到创建`struct`变量,访问`struct`成员等的语法,然后我们将看到一个完整的示例。 ### 如何声明结构的变量? ```c struct  struct_name  var_name; ``` 或者: ```c struct struct_name { DataType member1_name; DataType member2_name; DataType member3_name; … } var_name; ``` ### 如何使用结构变量访问结构的数据成员? ```c var_name.member1_name; var_name.member2_name; … ``` ### 如何为结构成员赋值? 有三种方法可以做到这一点。 1)使用点(`.`)运算符 ```c var_name.memeber_name = value; ``` 2)在一个声明中赋值所有成员 ```c struct struct_name var_name = {value for memeber1, value for memeber2 …so on for all the members} ``` 3)**指定初始值设定项** - 我们将在本文末尾讨论这个问题。 ### C 中的结构示例 ```c #include <stdio.h> /* Created a structure here. The name of the structure is * StudentData. */ struct StudentData{ char *stu_name; int stu_id; int stu_age; }; int main() { /* student is the variable of structure StudentData*/ struct StudentData student; /*Assigning the values of each struct member here*/ student.stu_name = "Steve"; student.stu_id = 1234; student.stu_age = 30; /* Displaying the values of struct members */ printf("Student Name is: %s", student.stu_name); printf("\nStudent Id is: %d", student.stu_id); printf("\nStudent Age is: %d", student.stu_age); return 0; } ``` 输出: ```c Student Name is: Steve Student Id is: 1234 Student Age is: 30 ``` ## C 中的嵌套结构:另一个结构中的结构 您可以在另一个结构中使用结构,这是相当可能的。正如我上面所解释的,一旦你声明了一个结构, **`struct struct_name`** 就像一个新的数据类型,所以你可以把它包含在另一个结构中,就像其他数据成员的数据类型一样。听起来很混乱?别担心。以下示例将清除您的疑问。 ### C 编程中嵌套结构的例子 假设我们有两个这样的结构: **结构 1:`stu_address`** ```c struct stu_address { int street; char *state; char *city; char *country; } ``` **结构 2:`stu_data`** ```c struct stu_data { int stu_id; int stu_age; char *stu_name; struct stu_address stuAddress; } ``` 正如你在这里看到的那样,我在另一个结构中嵌套了一个结构。 #### 结构内部结构的赋值(嵌套结构) 让我们以上面看到的两个结构为例来理解逻辑 ```c struct  stu_data  mydata; mydata.stu_id = 1001; mydata.stu_age = 30; mydata.stuAddress.state = "UP"; //Nested struct assignment .. ``` #### 如何访问嵌套结构成员? 使用`.`运算符链。 假设你想从嵌套结构中单独显示城市: ```c printf("%s",  mydata.stuAddress.city); ``` ## 在结构中使用`typedef` `typedef`使代码简短并提高了可读性。在上面的讨论中,我们已经看到,每次使用结构时我们都必须使用冗长的语法,这会使代码混乱,冗长,复杂且可读性降低。这个问题的简单解决方案是使用`typedef`。它就像`struct`的别名。 **没有`typedef`的代码** ```c struct home_address { int local_street; char *town; char *my_city; char *my_country; }; ... struct home_address var; var.town = "Agra"; ``` **使用`typedef`的代码** ```c typedef struct home_address{ int local_street; char *town; char *my_city; char *my_country; }addr; .. .. addr var1; var.town = "Agra"; ``` 每次需要声明`struct`变量时,不必使用`struct home_address`,只需使用`addr`,即我们定义的`typedef`。 ## 指定初始值设定项以设置结构成员的值 我们已经学会了两种设置`struct`成员值的方法,还有另一种方法可以使用指定的初始化器来完成相同的操作。当我们只赋值结构的少数成员时,这很有用。在以下示例中,结构变量`s2`只有一个赋值的成员。 ```c #include <stdio.h> struct numbers { int num1, num2; }; int main() { // Assignment using using designated initialization struct numbers s1 = {.num2 = 22, .num1 = 11}; struct numbers s2 = {.num2 = 30}; printf ("num1: %d, num2: %d\n", s1.num1, s1.num2); printf ("num1: %d", s2.num2); return 0; } ``` 输出: ```c num1: 11, num2: 22 num1: 30 ```