c中的结构体嵌套问题_C中的结构
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)
C language uses the struct
keyword to create a structure.
C语言使用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 data type.
在上面的代码片段中,我们创建了一个名为Student_info的结构来满足学生信息的目的。 在结构中,我们声明了数据成员: name , address , division和roll_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创建了结构变量S1和S2 。
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.
在上面的代码中,我们创建了结构变量S1和S2以及结构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 <stdio.h>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访问了结构变量: name , address , division和roll_num 。
Output:
输出:
The Student Information is as follows:Name: Safa Mulani
Address: Pune
Division: A
Roll Number: 24105
结构数组 (Array of Structure)
C allows the users to use an array 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<stdio.h>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: Safa
Division: A
Roll Number: 23105Enter Student Details:Name: Aman
Division: B
Roll Number:21042Enter Student Details:Name: Divya
Division: D
Roll Number: 2134Student Information:Name: Safa
Roll Number: 23105
Division: A
Name: Aman
Roll Number: 21042
Division: B
Name: Divya
Roll Number: 2134
Division: D
结构作为功能参数 (Structure as Function Parameters)
C enables programmers to pass a Structure as an argument to a function in a similar fashion of passing variables/arrays as arguments to functions.
C使程序员可以将结构作为参数传递给函数 ,方式类似于将变量/数组作为参数传递给函数。
Example:
例:
#include <stdio.h>
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 argumentreturn 0;
}
void display_details(struct Evaluation e1)
{printf("\nEvaluation details.....\n");printf("Name: %s", e1.name);printf("\nScore: %d", e1.score);
}
Output:
输出 :
Name: Safa
Score: 56Evaluation details.....
Name: Safa
Score: 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 pointer 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<stdio.h>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: Safa
Roll Number: 1234
结论 (Conclusion)
Thus, in this article, we have understood the concept of Structures in C language.
因此,在本文中,我们了解了C语言中的结构概念。
参考资料 (References)
- Structures in C documentationC文档中的结构
翻译自: https://www.journaldev.com/34614/structures-in-c
c中的结构体嵌套问题
c中的结构体嵌套问题_C中的结构相关推荐
- c语言结构体嵌套及输出,C语言结构体嵌套
C语言中的嵌套结构体表示在一个结构体中可以使用另一个结构作为成员.在C语言中定义结构体嵌套有两种方法: 通过独立的结构 通过嵌入式结构 1. 独立结构 我们可以创建2个结构体,但在主结构中应该使用依赖 ...
- Golang匿名结构体结构体嵌套(实战使用)
背景 最近研究钉钉机器人的时候,总是会向钉钉官方接口发送很多请求,发送请求的时候,需要封装body结构体传递参数,还要封装response结构体用来接收返回值,有时候,response结构体中还要嵌套 ...
- 结构体学习笔记6——结构体嵌套
结构体嵌套就是 在当前的结构体内的一个成员是另一个整体的结构体变量! struct Stu {char name[10];int age; }; struct Teach {char TeachNam ...
- C++ 基础入门 之 结构体/结构体定义和使用/结构体数组/结构体指针/ 结构体嵌套结构体/结构体做函数参数/结构体中 const 使用场景/结构体案例
C++ 基础入门 之 结构体/结构体定义和使用/结构体数组/结构体指针/ 结构体嵌套结构体/结构体做函数参数/结构体中 const 使用场景/结构体案例 目录 一.简单介绍 二.结构体定义和使用 三. ...
- c++结构体总结(结构体定义,结构体数组,结构体指针,结构体嵌套结构体,结构体做函数参数,结构体中 const使用场景)
看完b站黑马程序员之后的借鉴和笔记 1.什么是结构体,有什么作用? 在C/C++中,结构体是用户定义的数据类型.它可以把几种不同类型的数据项集合成结构体这样一个单一类型. 2. 结构体定义和使用 #i ...
- c++结构体嵌套结构体_Go学习每日一问(13)-结构体嵌套
每次学习并整理一个Golang的知识点,每天进步一点点.今天学习一个go结构体嵌套的知识点. 日省吾身 1.下面这段代码的输出结果? func main() { a := -7 b := +7 fmt ...
- java 结构体数组初始化_C数组结构体联合体快速初始化
背景 C89标准规定初始化语句的元素以固定顺序出现,该顺序即待初始化数组或结构体元素的定义顺序. C99标准新增指定初始化(Designated Initializer),即可按照任意顺序对数组某些元 ...
- 结构体嵌套结构体c语言,结构体的相互嵌套
编程时要用到C语言中结构体的自引用,由于用的少,学得时候也没在意,趁用到,回忆一下. 结构体的自引用(self reference),就是在结构体内部,包含指向自身类型结构体的指针. 结构体的相互引用 ...
- c/c++教程 - 1.10 结构体 使用typedef定义struct结构体 结构体数组 结构体指针 结构体嵌套 结构体做函数参数 结构体const
十二.结构体 (1)结构体定义和使用 基本概念:结构体属于用户自定义的数据类型,允许用户存储不同的数据类型. 参考视频:https://www.bilibili.com/video/BV1et411b ...
最新文章
- Android自定义View:MeasureSpec的真正意义与View大小控制
- Flash超链接修改工具
- 工作场合少聊事事非非
- cf1206解题报告
- 总结命令----tar
- CheckBox多选按钮实现CompoundButton.OnCheckedChangeListener
- 翁凯java进阶_多项式加法——mooc《零基础学Java语言》-(浙大翁凯)第五周编程题...
- 网络流量分类方法调研
- Easy Touch 5 简单使用
- LUOGU P1512 伊甸园日历游戏
- ajax authorization,ajax跨域,_ajax Authorization 鉴权失败,ajax跨域 - phpStudy
- 张良(?—前189年)
- 注册表 关闭打印机服务器,Win7系统添加打印机无Print Spooler服务无注册表解决方法...
- linux去除pdf页头,删除PDF水印小妙招
- 【Django入门】——模型管理器对象、模型管理器类和模型类
- error 1044 mysql_mysql ERROR 1044 (42000): Access denied for user ''@'localhost' to database
- 图像采集及处理多线程编程
- 《汇编语言与计算机系统组成》第六章例题-5
- 苹果电子邮件怎么注册_外贸技巧 | 6种简单的电子邮件营销技巧,你知道几个?...
- 基于ACCESS数据库写成的挂机游戏更新日志
热门文章
- 【linux】web socket
- 表迁移工具的选型-复制ibd的方法
- 配置Hadoop开发环境(Eclipse)
- layout_gravity与gravity的区别
- Activity与Intent机制的学习笔记--转自feisky
- 单工、半双工、双工通信详解
- 02: python3使用email和smtplib库发送邮件
- @media实现网页自适应中的几个关键分辨率
- fshc模块fsch2mcu_if理解
- (七)图像处理中常用算子Laplacian\Sobel\Roberts\Prewitt\Kirsch