Key Difference – Structure vs联盟在c
An array是由支持的数据结构化。C language.An array can be used to store data elements of the same type. If there is a statement as int marks [10]; then marks are an array that can store ten marks and all of them are integers. Sometimes it is required store data elements of different types in the same memory location. For example, an employee can have an employee ID, name, department, age etc. They are of different data types. Therefore, it is necessary to have a method to store various data elements as a single unit. Structures and Unions in C are used for storing data elements of different types in the same memory location.A structure and a union are similar but they mainly differentiate due to memory allocation.存储结构变量所需的内存是所有成员的存储器大小的总和。存储联合变量所需的内存是联合最大元素所需的内存。That is关键区别本文中的结构与联合之间的结构讨论了C中的结构与联合之间的差异。
CONTENTS
1。概述和关键差异
2。C中的结构是什么
3。What is Union in C
4.Similarities Between Structure and Union in C
5.Side by Side Comparison – Structure vs Union in C in Tabular Form
6.Summary
C中的结构是什么?
结构是C中的用户定义数据类型。它有助于组合不同类型的数据项。结构可以表示记录。学生可以使用Student_ID,student_name等。而不是分别存储每个变量,所有这些不同的数据项都可以使用结构紧凑到一个单元中。它是使用关键字“ struct”定义的。在结构中,可以随时访问其所有成员。以下是派生的数据类型结构学生。
struct Student {
intstudent_id;
char student_name[20];
};
For the above structure, variables can be declared as follows.
struct Student student1, student2, student3;
There are two methods to access the members of the structure. That is by using the member operator (.) and structure pointer operator (->). The members can be accessed using structure_variable_name. member name. If the programmer wants to access the name of the student 2, then he can write the statement as printf(student2.student_namename);
Refer the below program with a structure.
根据上述计划,学生是一个结构。它包含Student_id和student_name。在主程序中声明了两个结构类型的变量。它们被称为student1和student2。使用成员操作员作为student1.student_id = 1,将Student1的ID分配为1。名称“ Ann”是一个字符串。因此,使用字符串副本函数strcpy复制给Student_name成员。ID和名称以类似的方式分配给Student2。最后,这些值是使用成员操作员打印的。
The amount of memory required to store a structure variable is the sum of the memory size of all members. The student_id contains 4 bytes and student_name contains 20 bytes (one byte each for a character). The total 24 bytes is the sum of memory size required by the structure.
What is Union in C?
A union is a user-defined data type in C. It helps to store different data types in the same memory location. A Book can have properties such as book_name, price etc. Instead of creating variables for each of them, a union can be used to compact all different data types into a one unit using a union. It is defined using the keyword ‘union’.The following creates a derived data union Book.
unionBook{
char name[20];
double price;
};
对于上述工会,可以声明变量如下。
union Book book1, book2;
有两种访问联盟成员的方法。那就是使用成员操作员(。)和结构指针操作员( - >)。可以使用Union_variable_name访问成员。成员名字。如果程序员想访问book1的名称,则可以将语句写为printf(book1.name);
请参阅下面的计划。
根据上述程序,这本书是工会。Book1是类型联合的变量。名称和价格分配值。在工会中,一次只能访问其成员中的一个,所有其他成员都将具有垃圾价值。ID的值无法正确打印,但价格价值打印得正确。
根据上述程序,书是一个工会。Book1和book2是联合类型变量。首先,分配了Book1名称的值并打印。然后分配了Book2名称的值,并打印出来。所有成员都正确打印,因为一次使用一个成员。存储联合所需的内存是联合最大元素所需的内存。在上面的程序中,名称变量为20个字节。它比价格大。因此,联盟的内存分配是20个字节。
What are the Similarities Between Structure and Union in C?
- C中的结构和联合都是用户定义的数据类型。
- 结构和联合C可用于存储different data types in the same memory location.
C中的结构和联合有什么区别?
结构与C的联合 |
|
结构是C语言中用户定义的数据类型,允许将不同类型的数据组合在一起。 | Union是C语言中用户定义的数据类型,允许将不同类型的数据组合在一起。 |
Accessibility | |
In a structure, all its members can be accessed at any time. | In a union, only one of its members can be accessed at a time and all other members will contain garbage values. |
Memory Allocation | |
存储结构变量所需的内存是所有成员的存储器大小的总和。 | 存储联合变量所需的内存是联合最大元素所需的内存。 |
关键词 | |
The keyword used to define a structure is ‘struct’. | 用于定义工会的关键字是“联盟”。 |
概括 -结构与联盟在c
数组用于存储相同类型的数据元素。有时,有必要在同一内存位置存储不同类型的数据元素。C编程语言提供结构和联合来完成此任务。两者都是用户定义的数据类型。存储结构变量所需的内存是所有成员的存储器大小的总和。存储联合变量所需的内存是联合最大元素所需的内存。这是C中的结构与联合之间的差异。
参考:
1。Point, Tutorials. “Structures in C.”,教程点,,,,15 Aug. 2017.在这里可用
2。Point, Tutorials. “Unions in C.”,,,,教程点,,,,15 Aug. 2017.在这里可用
Leave a Reply