Union

 

typedef struct in C

The C language contains the typedef keyword to allow users to provide alternative names for the primitive (e.g.,​ int) and user-defined​ (e.g struct) data types.

which you can use to give a type a new name

example to define a term BYTE for one-byte numbers

typedef unsigned char BYTE;

After this type definition, the identifier BYTE can be used as an abbreviation for the type unsigned char,

 example.

BYTE b1, b2;

 

Self-referential structures

Self-Referential structures are those structures that have one or more pointers which point to the same type of structure, as their member. In other words, structures pointing to the same type of structures are self-referential in nature.

These structures can have only one self-pointer as their member. The following example will show us how to connect the objects of a self- referential structure with the single link and access the corresponding data members.

 




Unions

 

In C, a union is a user-defined data type that allows many different data types to be stored in the same memory region. A union can have numerous members, but only one of them can occupy the memory at any one moment. unions, only store information in one field at once.

Syntax-:

union unionName {

   member definition;

   member definition;

   .

   .

   .

   member definition;

} [unionVar1, unionVar2, ...]; //where unionVar's are union variables














Difference Between Structure and Union 


Structure

Union

A user can deploy the keyword struct to define a Structure.

A user can deploy the keyword union to define a Union.

The implementation of Structure in C occurs internally- because it contains separate memory locations allotted to every input member.

In the case of a Union, the memory allocation occurs for only one member with the largest size among all the input variables. It shares the.

A user can access individual members at a given time.

A user can access only one member at a given time.

 Syntax

struct [structure name]

{

type element_1;

type element_2;

.

.

} variable_1, variable_2, …;

Syntax

union [union name]

{

type element_1;

type element_2;

.

.

} variable_1, variable_2, …;

A Structure does not have a shared location for all of its members. It makes the size of a Structure to be greater than or equal to the sum of the size of its data members.

A Union does not have a separate location for every member in it. It makes its size equal to the size of the largest member among all the data members.

In the case of a Structure, there is a specific memory location for every input data member. Thus, it can store multiple values of the various members.

In the case of a Union, there is an allocation of only one shared memory for all the input data members. Thus, it stores one value at a time for all of its members.

 

SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Problems Classification

  Problems Classification Problem classification is the process of assigning a level, based on predefined criteria. Problem classification h...