📜  C语言中的数组和联合之间的区别

📅  最后修改于: 2021-05-26 02:17:22             🧑  作者: Mango

1. C中的数组:
数组是通过存储在连续内存位置的通用名称访问的相似数据项的集合。数组的元素可以使用索引进行访问。它们可以用于存储原始数据类型,例如int,float,double,char等,但是所有元素必须具有相同的数据类型。下面是一个如画的数组表示。

数组声明:

datatype array_name[size]

2. C联合:
联合是用户定义的数据类型,它允许将异构元素存储在同一存储位置中。联合的大小是联合中最大元素的大小。下面是工会的如画插图。

工会声明:

union name
{
  datatype element;
  datatype element;
};

数组和联合之间的区别:

ARRAY UNION
Collection of elements of same data types. Collection of elements of heterogenous data types.
Arrays can be one or two dimensional. Unions do not have type.
Each element is allocated a specific memory location. The elements share the memory location which has size equivalent to the size of the largest size element of the union.
All members can contain value at a given time. Only one member can contain value at a given time.
Not an efficient use of memory as all members are allocated different memory locations. Efficient use of memory as all members do not require separate memory location.
Array elements can be accessed using index. The elements of a union cannot be accessed using index.

Syntax :

datatype array_name[size]

Syntax :
Union user defined name

{

datatype Variable 1; datatype variable2;

};

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。