博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c中的结构体嵌套问题_C中的结构
阅读量:2530 次
发布时间:2019-05-11

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

c中的结构体嵌套问题

Structures in C language are basically user-defined data types that enables the user to create a data type that can group elements of different data types into it.

C语言的结构基本上是用户定义的数据类型 ,使用户能够创建一个数据类型,数据类型可以将不同数据类型的元素分组到其中

Thus, it contains different data types to be represented by a single Structure name.

因此,它包含由单个结构名称表示的不同数据类型。



用C创建结构 (Creating Structures in C)

uses the struct keyword to create a structure.

使用struct关键字创建结构。

Syntax:

句法:

struct Structure_Name {    Datatype data_member1;    Datatype data_member2;    .    .    Datatype data_memberN;};

As mentioned above, C language uses the struct keyword to build a structure. Inside the curly brackets, the user can define the data members necessary to serve the purpose of the particular program. These data members are the basic C language variables of different data types such as int, float, double, char, etc.

如上所述,C语言使用struct关键字来构建结构。 在大括号内,用户可以定义实现特定程序目的所需的数据成员。 这些数据成员是不同数据类型(例如int,float,double,char等)的基本C语言变量。

It is mandatory to add a semi-colon (;) after the closing curly bracket of a particular structure.

强制在特定结构的右花括号后添加分号(;)。

Example:

例:

struct Student_info{ char name[100]; char address[100]; char division[50]; int roll_num; };

In the above code snippet, we have created a structure as Student_info to serve the purpose of Student Information. Within the structure, we have declared data members: name, address, division, and roll_num with their particular .

在上面的代码片段中,我们创建了一个名为Student_info的结构来满足学生信息的目的。 在结构中,我们声明了数据成员: nameaddressdivisionroll_num及其特定的 。



声明结构变量 (Declaration of Structure Variables)

Structure Variables enable the user to access the data-members declared inside the structure.

使用结构变量 ,用户可以访问在结构内部声明的数据成员。

Either of the following ways can be used to declare Structure Variables in C:

可以使用以下任何一种方法在C中声明结构变量:

  • Declaration of Structure variables after the Structure definition

    在结构定义之后声明结构变量
  • Declaration of Structure variables along with the Structure definition

    结构变量的声明以及结构定义

1.在结构定义之后声明结构变量 (1. Declaration of Structure variables after the Structure definition)

Syntax:

句法:

struct Structure_Name {    Datatype data_member1;    Datatype data_member2;    .    .    Datatype data_memberN;};struct Structure_Name Variable1, Variable2,.., VariableN;

Example:

例:

struct Student_info{ char name[100]; char address[100]; char division[50]; int roll_num; };struct Student_info S1, S2;

In the above snippet of code, we have created Structure Variables S1 and S2 for the Structure Student_info after the declaration of the structure.

在上面的代码片段中,我们在结构声明后为结构Student_info创建了结构变量S1S2

2.结构变量的声明以及结构定义 (2. Declaration of Structure variables along with the Structure definition)

Syntax:

句法:

struct Structure_Name {    Datatype data_member1;    Datatype data_member2;    .    .    Datatype data_memberN;} Structure_variable1, Structure_variable2;

Example:

例:

struct Student_info{ char name[100]; char address[100]; char division[50]; int roll_num; }S1, S2;

In the above piece of code, we have created the Structure Variables S1 and S2 along with the declaration of the structure Student_info.

在上面的代码中,我们创建了结构变量S1S2以及结构Student_info的声明。



初始化结构数据成员 (Initializing Structure Data Members)

The C Structure data members cannot be initialized while declaring them. They need to be separately assigned values after the declaration of data members of the structure.

声明它们时,不能初始化C结构数据成员。 在声明结构的数据成员之后,需要为它们分别分配值。

Example: Declaring Structure Data members while declaring them gives an error

示例:在声明结构数据成员的同时声明它们会产生错误

struct Student_info{ char *name = "Safa"; // This statement gives compilation error };

As mentioned above, the Structure data members need to be initialized separately after the declaration.

如上所述,在声明之后,需要分别初始化Structure数据成员。

Syntax:

句法:

Structure_Name.Structure_data_member_name = Value


访问结构中的数据成员 (Accessing data members in a Structure )

C uses the period or member access operator (.) to access the data members of the structure.

C使用句点或成员访问运算符(。)来访问结构的数据成员。

Syntax:

句法:

Structure_variable_name.Structure_data_member_name

Example:

例:

#include 
struct Student_info{ char *name; char *address; char *division; int roll_num; };int main(){ struct Student_info S1; S1.name = "Safa Mulani"; S1.address = "Pune"; S1.division = "A"; S1.roll_num = 24105; printf("The Student Information is as follows:\n"); printf("\nName: %s", S1.name); printf("\nAddress: %s", S1.address); printf("\nDivision: %s", S1.division); printf("\nRoll Number: %d", S1.roll_num); return 0;}

Here, we have created variable S1 of type struct. Further, we have accessed the structure variables: name, address, division and roll_num using S1.

在这里,我们创建了类型为struct的变量S1。 此外,我们已经使用S1访问了结构变量: nameaddressdivisionroll_num

Output:

输出:

The Student Information is as follows:Name: Safa MulaniAddress: PuneDivision: ARoll Number: 24105


结构数组 (Array of Structure)

C allows the users to use an to represent the data elements of the Structure.

C允许用户使用来表示结构的数据元素。

Syntax:

句法:

struct Structure_Name array_name[size];

In the below snippet of code, we have created an array ‘S’ of structure Student_info with size = 3.

在下面的代码片段中,我们创建了一个结构为Student_info大小为3的数组“ S”

Example:

例:

#include
struct Student_info{ char name[100]; char division[50]; int roll_num; };void main(){struct Student_info S[3];for(int i = 0; i < 3; i++) { printf("\nEnter Student Details:\n"); printf("\nName:\t"); scanf("%s", S[i].name); printf("\nDivision:\t"); scanf("%s", S[i].division); printf("\nRoll Number:\t"); scanf("%d", &S[i].roll_num); } printf("\nStudent Information:\n"); for(int i = 0; i < 3; i++) { printf("\nName: %s", S[i].name); printf("\nRoll Number: %d", S[i].roll_num); printf("\nDivision: %s", S[i].division); }}

Output:

输出:

Enter Student Details:Name: SafaDivision: ARoll Number: 23105Enter Student Details:Name: AmanDivision: BRoll Number:21042Enter Student Details:Name: DivyaDivision: DRoll Number: 2134Student Information:Name: SafaRoll Number: 23105Division: AName: AmanRoll Number: 21042Division: BName: DivyaRoll Number: 2134Division: D


结构作为功能参数 (Structure as Function Parameters)

C enables programmers to pass a Structure as an argument to a in a similar fashion of passing variables/arrays as arguments to functions.

C使程序员可以将结构作为参数传递给 ,方式类似于将变量/数组作为参数传递给函数。

Example:

例:

#include 
struct Evaluation{ char name[50]; int score;};void display_details(struct Evaluation e1);int main(){ struct Evaluation e; printf("Name: "); scanf("%[^\n]%*c", e.name); printf("Score: "); scanf("%d", &e.score); display_details(e); // passing structure as an argument return 0;}void display_details(struct Evaluation e1) { printf("\nEvaluation details.....\n"); printf("Name: %s", e1.name); printf("\nScore: %d", e1.score);}

Output:

输出

Name: SafaScore: 56Evaluation details.....Name: SafaScore: 56


嵌套结构 (Nested Structures)

Nested Structures in C basically defining one structure into another structure i.e. it permits one structure to have another structure as a variable.

C中的嵌套结构基本上将一个结构定义为另一个结构,即它允许一个结构具有另一个结构作为变量

Example:

例:

struct Student_info{ char *name; char *Branch_name;struct Evaluation    {   char *division;         int hsc_score;        int ssc_score;        int roll_num; 		    }eval;}student;

In the above snippet, We have created a structure Student_info wherein we have represented another structure Evaluation as a member of it.

在以上代码段中,我们创建了一个Student_info结构,其中我们代表了另一个结构Evaluation作为其成员。

You can access the nested structs using struct1_var.struct2_var.struct2_innervar

您可以使用struct1_var.struct2_var.struct2_innervar访问嵌套结构

For example, in the above example, we used student as struct 1 variable and eval for the nested struct. We can access the inner variables as student.eval.ssc_score;

例如,在上面的示例中,我们使用Student作为结构1变量,并使用eval来嵌套结构。 我们可以将内部变量访问为student.eval.ssc_score;。



结构指针 (Pointers in Structure)

C allows us to have a pointer to a structure. The arrow (->) operator enables the user to access the data members of the structure having a to it.

C允许我们有一个指向结构的指针。 箭头(->)运算符使用户可以访问具有它的结构的数据成员。

Syntax:

句法:

struct Structure_Name {    Datatype data_member1;    Datatype data_member2;    .    .    Datatype data_memberN;};int main(){    struct Structure_Name *pointer_name;}

Example:

例:

#include
struct Student_info{ char *name;int roll_num; };int main(){struct Student_info *St, S;St = &S; printf("\nEnter Student Details:\n"); printf("\nName:\t"); scanf("%s", St->name); printf("\nRoll Number:\t"); scanf("%d", &St->roll_num); printf("\nStudent Information:\n"); printf("\nName: %s", St->name); printf("\nRoll Number: %d", St->roll_num); return 0;}

Output:

输出:

Enter Student Details:Name:	SafaRoll Number:	1234Student Information:Name: SafaRoll Number: 1234


结论 (Conclusion)

Thus, in this article, we have understood the concept of Structures in C language.

因此,在本文中,我们了解了C语言中的结构概念。



参考资料 (References)

翻译自:

c中的结构体嵌套问题

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

你可能感兴趣的文章
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_02.程序间耦合_4 曾经代码中的问题分析
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>