Structures in C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

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:

1.By struct keyword within main() function.

2.By declaring a variable at the time of defining the structure.


1. By struct keyword within a main function.

• We can declare variables in main function. Let us discuss with an example.

struct stud { struct structurename{


int rollno; data_type member1;
char name[20]; data_type member2;
float marks; .
}; data_type member n;
int main() { };
struct stud s1; mani function() {
struct structername variable1,2..;
} }
By declaring a variable at the time of defining a structure.

• Declaring a variable at the time of defining a structure. It is easy and


simple.
• Syntax:
struct structure_name { struct student{
data_type element; int rno;
data_type element; float marks;
. }s1,s2;
.
} structure variable1,structure variable2,..structure variable n;
• Accessing Structure variable
There are two ways to access structure members:
1.By . (member or dot operator).
2.By -> (structure pointer operator).

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.

Syntax: typedef <existing_name> <alias_name>


In the above syntax, 'existing_name' is the name of an already existing variable
while 'alias name' is another name given to the existing variable.
suppose we want to create a variable of type unsigned int, then it becomes a tedious
task if we want to declare multiple variables of this type. To overcome the problem,
we use a typedef keyword.
Ex: typedef unsigned int unit;
we have declared the unit variable of type unsigned int by using a typedef keyword.
Using typedef with structures.
struct student{
int rno;
float marks;
char name[20];
};
If we want to declare a structure variable to student we have to follow this
struct student s1,s2;
But we can avoid this with the use of typedef.
Syntax: typedef struct structure_name alias_name;
i.e typedef struct student stud;
Here student remained as stud. So we can declare variables as
stud s1,s2;
Array of structures.
An array of structures in C can be defined as the collection of multiple structures
variables where each variable contains information about different entities. The
array of structures in C are used to store information about multiple entities of
different data types. The array of structures is also known as the collection of
structures.
• Syntax:
struct structure_name svariable[size];
Ex:
#include<stdio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
};
int main(){
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++){
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++){
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
Nested Structure.
• C provides us the feature of nesting one structure within another structure by using which, complex data
types are created.
• To store the address of the employee, we need to store the address of the employee into a separate
structure and nest the structure address into the structure employee.
Ex:
#include<stdio.h>
struct address
{
char city[20];
int pin;
char phone[14];
};
struct employee
{
char name[20];
struct address add;
};
Types of Nested Structure

• 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

• We can access the member of the nested structure by


Outer_Structure.Nested_Structure.member.

• 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.

Syntax: functionName(structure_variable.memberName); //function call


Ex: display(emp.name);
Passing entire structure into function.
We can pass entire structure into function and access by calling the function.
Syntax: functionName(structureVariable);
Ex: display(emp);
Call by reference

• Passing address of structure into function.


• When passing a structure to a function by reference, a pointer to the structure is
passed instead of a copy of the entire structure. This allows you to manipulate the
structure's members directly, making it more efficient (especially for large
structures) and enabling the function to modify the original structure.
• Syntax:
void functionName(struct StructureName *ptr) {
// Access members using the arrow operator (->)
ptr->memberName = value;
}
Ex:
#include <stdio.h>
struct Student {
int id;
char name[50];
float marks;
};
void updateStudent(struct Student *s) {
// Modifying structure members using the arrow operator
s->id = 102;
strcpy(s->name, "Bob");
s->marks = 95.0;
}
int main()
{
struct Student s1 = {101, "Alice", 89.5};
printf("Before update:\n");
printf("ID: %d, Name: %s, Marks: %.2f\n", s1.id, s1.name, s1.marks);
// Passing structure by reference
updateStudent(&s1);
printf("After update:\n");
printf("ID: %d, Name: %s, Marks: %.2f\n", s1.id, s1.name, s1.marks);
return 0;
}
• Comparison : call by value and call by reference
Feature Call by Value Call by Reference
The address of the structure is
Copy or Address A copy of the structure is passed.
passed.
Memory Usage Higher (entire structure is copied). Lower (only pointer is passed).
Changes affect the original
Modification Scope Changes are local to the function.
structure.
Access Dot operator (.) is used. Arrow operator (->) is used.
Pointers of Structures

• In C, pointers to structures are used to refer to the memory address of a


structure. This is especially useful when you want to pass structures to functions
efficiently, work with dynamic memory allocation, or manipulate large structures
without copying them.
Syntax for declaration:
• struct StructName *pointerName;
Accessing variable:
When accessing structure members via a pointer, you use the arrow operator (->).
The arrow operator is shorthand for dereferencing the pointer and then using the dot
operator.
Ex:
#include <stdio.h>
#include <stdlib.h>

// 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;
}

You might also like