• admin@embedclogic.com

Structure in ‘C’

What is Structure?

In C language, an array is used to collect same data type values under a single variable name at contiguous memory locations but what happens if we need to collect different-2 datatype Values under the same roof. For example, you want to maintain the attendance record of employees of any company. Attendance record of an employee will contain following attributes

name         2. employeId           3. attendance

All above attributes of record need different-2 data type as shown in below table.

Information DataType
Namestring(Array of char dataType)
employeIdunsigned int
Salarydouble

So as we can see above all the members have the different-2 data type and if we initialize these individually then we can never get the right information of any employee because we don’t know where the requested field has stored. so in this situation, C gives a unique data type that will keep all the members [of same data type or heterogeneous data type] under the master data type which is called ‘struct‘ and concept is known as ‘structure’.

so ‘struct’ data type is secondary data type which is the collection of homogeneous or heterogeneous data types.

How to define it?

Syntax:

Generally, syntax used for structure declaration is-

where tag_name is optional.

Declaration using typedef:
OR
Both above declarations are same.

Example :

in above structure, all three members of different -2 data types are packed into a single tag_name ’emp_attendance’ of data type ‘struct’.

Now one can get all the members through a single pointer because all the member of a structure is stored in contiguous locations of memory.

structure initialization:

Case 1:

We have multiple ways to initialize a structure.As shown in above code

This initialization is valid when we initialized elements are at the contiguous location.in above example –

“Amit” will initialize first member,12345 will 2nd and ‘P’ will initialize next member of the structure.

Case 2:

we can use below format If data members are not in an order

Case 3:

We can initialize a structure by assigning data of another member of the same structure type.

emp2 and emp1 is variable of same type structure so we can initialize emp2 this way.

 structure member access:
1. Using (.) operator
syntax: 

for eg. – emp1.name

Example Program:

output : 

2. Using pointer-

syntax: 

The syntax for structure member access is –

for eg. –       emp1->name

Example Program:

output : 

Nested C Structure:

Nesting means “Structure within another structure”.

Example:

Output:

Array in Structure:

A collection of similar data type structure in a row at contiguous memory locations under the single variable name with different indexes is known as an array of structure.

Syntax:

 struct tag_name structure_variable[Number_OF_STRUCTURE];

struct student std[5];

Here ”std” is an array of 5 student type structure.

Example:

code to collect information about 5 students.
Output:
Quick links:
typedef use in C Structure
Structure Size, Alignment, Padding, and Packing
Bitfield in C structure

"Knowledge is never useless"

Subscribe and stay updated with our latest articles.

 

 

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

Embedclogic will use the information you provide on this form to be in touch with you and to provide updates and marketing.