C Structures
C Structures
In C, a structure (often abbreviated as struct) is a user-defined data type that allows you to group variables of
different types under a single name. Structures are useful for organizing related data and making code more
readable.
C Structure Declaration:
Syntax
struct structure_name {
data_type member_name1;
data_type member_name1;
....
};
C Structure Definition:
1. Structure Variable Declaration with Structure Template
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
}variable1, varaible2, ...;
Example:
#include<stdio.h>
struct student{
int roll_no;
char std_name[20];
float marks;
};
void main()
{
struct student s1={1, "Ritik", 75.4};
struct student s2;
printf("Enter the values for s2=\n");
scanf("%d%s%f", &s2.roll_no, &s2.std_name, &s2.marks);
printf("The details of students\n");
printf("s1 = %d\t%s\t%f\n",
n", s1.roll_no, s1.std_name, s1.marks);
printf("s2 = %d\t%s\t%f\n",
n", s2.roll_no, s2.std_name, s2.marks);
}
Output:
Key Points
1. Member Variables:: A structure can contain multiple members of different data types.
2. Memory Layout:: The members of a structure are laid out in memory in the order they are
defined.
3. Accessing Members:: Members are accessed using the dot (.) operator when you have a
structure variable.
4. Passing Structures:: Structures can be passed to functions by value or by reference (using
pointers), which can simplify function signatures.
The pictorial representation of structure memory allocation is given below. This diagram will help you
to understand the memory allocation concept in C very easily.
Nested Structures:
A nested structure in C is a structure within structure. One structure can be declared inside another structure in the
same way structure members are declared inside a structure.
#include <stdio.h>
#include <string.h>
// Declaration of the dependent structure
struct Employee
{
int employee_id;
char name[20];
int salary;
};
// Declaration of the Outer structure
struct Organisation
{
char organisation_name[20];
char dept_number[20];
struct Employee emp1;
};
int main()
{
// Structure variable
struct Organisation org;
printf("The size of structure organisation : %ld
%ld\n", sizeof(org));
org.emp1.employee_id = 101;
strcpy(org.emp1.name, "Amit Kumar");
org.emp1.salary = 40000;
strcpy(org.organisation_name, "JBPIC");
strcpy(org.dept_number, "T_1");
Let's see an example of an array of structures that stores information of 5 students and prints it.
#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;
}
Output:
Passing structures to functions: We can pass structures to functions as arguments or return them from
functions.
#include<stdio.h>
#include<string.h>
struct student
{
int id;
char name[20];
int marks;
};
int main()
{
struct student s1;
void display(struct student s1);
printf("Enter id of student s1 = \n");
n");
scanf("%d", &s1.id);
printf("Enter name of student s1 = \n");
\
scanf("%s", s1.name);
printf("Enter marks of student s1 = \n");
scanf("%d", &s1.marks);
display(s1);
return 0;
}
void display(struct student x)
{
printf("Details of Student is as following:
following:\n");
printf("%d\t%s\t%d\t",t", x.id, x.name, x.marks);
}
Output:
Advantages and Disadvantages of structure in C: C Structures in C provide a way to group different data
types together, offering several advantages and disadvantages:
Advantages
1. Grouping Data:: Structures allow you to group related data together, making it easier to manage
complex data types. For example, a struct can represent a Point with x and y coordinates.
2. Improved Code Organization:: Using structures can lead to more organized and readable code, as
related data is encapsulated in one entity.
3. Easy to Pass Around:: Structures can be passed to functions as a single argument, simplifying
simpli
function signatures and improving code clarity.
4. Flexibility:: Structures can be nested, meaning you can have a structure within another structure,
allowing for more complex data representations.
5. Memory Efficiency:: Unlike classes in C++, structures do not have a virtual table (vtable), which can
make them more memory-efficient
efficient for certain use cases.
Disadvantages
1. No Methods:: Structures in C do not support member functions or methods, limiting their
functionality compared to classes in C++.
2. No Access Control:: Structures lack access control mechanisms (like private and protected
members), which can lead to unintended data manipulation.
3. Manual Memory Management
Management:: You must manage memory allocation and deallocation manually,
which can lead to memory leaks iif not handled carefully.
4. Performance Overhead:: Passing large structures to functions can incur performance overhead,
especially if passed by value rather than by reference.
5. Limited Functionality:: Structures in C are primarily data holders, so you must rely on external
functions to operate on the data, which can complicate the codebase.