Table of Contents
In this lesson, you will learn about Structures in C, their usage, with the help of examples to better understand the topic.
A Structure or struct is a user-defined data type in programming. It holds different types of data under a single unit variable. It is similar to an array; however, An array can only store elements with a matching data type. On the other hand, a C structure can hold different data types and works whole as a single data type. It also helps to keep a record of various dimensions of data.
Create a structure in C
struct EmployeeInfo {
char Name[30];
int ID;
int age;
char Department[20];
double Salary;
};
Declare a Structure Variable
// A variable declaration with structure declaration.
struct EmployeeInfo {
data_member;
data_member;
}
emp1, emp2; // The variable emp1, and emp2 is declared with 'EmployeeInfo'
// A variable declaration like basic data types
struct EmployeeInfo {
data_member;
data_member;
};
int main() {
struct EmployeeInfo emp1, emp2; // The variable emp1 and emp2 is declared like a normal variable
}
Initialize Struct Members
Struct members are a bit different and tricky when it comes to initialization. We cannot initialize a struct member directly like regular variables.
struct EmployeeInfo {
int ID = 1;
double Salary = 25000.00;
};
The above initialization occurs a compilation error. When we declare a data type, no memory is allocated for it. The memory only is allocated when we create a variable. To initialize a struct member, we have to put the value inside a curly brace. Have a thorough look at the below example.
struct EmployeeInfo {
int ID;
double Salary;
};
struct EmployeeInfo emp1 {
1,
25000.00
};
Access Struct Members
To access any member from a C structure, we must use the member access operator (.)
#include <stdio.h>
#include <string.h>
struct EmployeeInfo {
char * Name;
int ID;
int Age;
char * Department[20];
float Salary;
};
int main() {
struct EmployeeInfo emp1, emp2;
//Initializing members
emp1.Name = "Adam";
emp1.ID = 1;
emp1.Age = 35;
strcpy(emp1.Department, "Finance");
emp1.Salary = 25000.0;
// Showing the members value on computer's screen
printf("\nEmployee Name is: %s", emp1.Name);
printf("\nEmployee ID is: %d", emp1.ID);
printf("\nEmployee's age is: %d", emp1.Age);
printf("\nEmployee's Department is: %s", emp1.Department);
printf("\nEmployee's Salary is: %2f", emp1.Salary);
}
Output
Employee Name is: Adam
Employee ID is: 1
Employee’s age is: 35
Employee’s Department is: Finance
Employee’s Salary is: 25000.00.
We can create more structured variables for a specific employee of a certain company and get their details within seconds.
Summary
- Structures assemble different kinds of data types in it and bind them together to the same place.
- It is easy to understand, and a time-saving approach alongside.
- Structures are more feasible to handle and user friendly.
- It does not permit data hiding. Therefore, its members can be reached from anywhere in the scope of the structure.
