Structures in C
Structures in C
Structures in C
Structures
• Structure in c is a user-defined data type that enables us to store the collection of
different data types. Each element of a structure is called a member.
• Structure defined by struct keyword.
• Syntax:
struct structure_name
{
data_type element1;
data_type element 2; // members of structure.
.
.
data_type element n;
};
Ex
struct employee
{ int id;
char name[20];
float salary;
};
Memory allocation for above example.
Declaring a structure variable
• We can declare a variable for the structure so that we can access the member of the
structure easily. There are two ways to declare structure variable:
By . Dot operator.
The dot operator is used only when you are working with a structure variable directly (not a pointer to a
structure).
Syntax:
StructureVariable.membername;
• Arrow operator.
The arrow operator (->) in C is used to access members of a structure through a
pointer to that structure. It's a shorthand for dereferencing a pointer and then
accessing the structure member.
syntax: structurevariable->membername;
Ex: ptr->id = value;
• Without arrow(->) operator
(*ptr).id = value;
Typedef in C
• The typedef is a keyword used in C programming to provide some meaningful
names to the already existing variable in the C program. It behaves similarly as we
define the alias for the commands. In short, we can say that this keyword is used
to redefine the name of an already existing variable.
• Separate Structure
• Embedded Structure
Separate Structure
Here, we create two structures, but the dependent structure should be used inside the
main structure as a member.
• Ex:
struct Date
{
int dd;
int mm; Dependent Structure.
int yyyy;
};
struct Employee
{
int id;
char name[20];
struct Date doj;
}emp1;
Embedded Structure
• The embedded structure enables us to declare the structure inside the structure. Hence, it requires
less line of codes but it can not be used in multiple data structures.
Ex:
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm; sub structure date in main structure employee.
int yyyy;
}doj;
}emp1;
Accessing Nested structure
• Ex:
e1.doj.dd;
Passing structure to function
• Just like other variables, a structure can also be passed to a function. We may pass
the structure members into the function or pass the structure variable at once.
Consider the following example to pass the structure variable employee to a
function display() which is used to display the details of an employee.
Ex:
#include<stdio.h>
struct address
{
char city[20];
int pin;
char phone[14];
};
struct employee
{
char name[20];
struct address add;
};
void display(struct employee); //passing structure into function.
void main ()
{
struct employee emp;
printf("Enter employee information?\n");
scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin, emp.add.phone);
display(emp); // calling structure variable.
}
void display(struct employee emp) // function definition and passing
structure into function.
{
printf("Printing the details....\n");
printf("%s %s %d %s",emp.name,emp.add.city,emp.add.pin,emp.add.phone);
}
Types of passing structures into function.
• Call by value.
• Passing individual members into function.
• Passing entire structure into function.
• Call by reference.
• Passing address of structure into function.
Passing members into function.
• We can pass only members which we want to pass the members. It is useful when
structure have many members and desire to pass specific members into function.
// Defining a structure
struct Student {
int id;
float marks;
};
int main() {
// Dynamically allocate memory for a structure
struct Student *ptr = (struct Student *)malloc(sizeof(struct Student));
// Assigning values using the pointer
ptr->id = 102;
ptr->marks = 92.5;
// Printing structure members
printf("ID: %d\n", ptr->id);
printf("Marks: %.2f\n", ptr->marks);
// Freeing the allocated memory
free(ptr);
return 0;
}