In C, after arrays, structures are the most important data structure available for programmers.
We shall understand different ways to initialize memory to structure and accessing elements inside a structure.
Creating simple structure and accessing elements inside them
In this example, we shall create a structure variable inside main and allocate memory in stack.
Below is a “student” structure that has 2 elements. A char array to hold name and int variable to hold roll number.
Then we create a structure variable inside main, so it will create memory inside stack.
Then as we have statically allocated memory, we can access the elements by dot “.” operators.
Example:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student_database
{
char name[30];
int roll_no;
};
int main()
{
struct student_database stud = {"jane", 2};
printf("Student name = %s \n", stud.name);
printf("Student roll no = %d \n", stud.roll_no);
return 0;
}
Output:
Student name = jane
Student roll no = 2
Dynamically allocating memory for structure and accessing elements inside them
To dynamically allocate memory you need to use malloc.
This will allocate memory in heap segment.
To access the elements, you need to use arrow operator “->”.
Example:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student_database
{
char name[30];
int roll_no;
};
int main()
{
struct student_database *stud = NULL;
stud = (struct student_database*) malloc(sizeof(struct student_database));
stud->roll_no = 30;
strcpy(stud->name, "Jane Doe");
printf("Student name = %s \n", stud->name);
printf("Student roll no = %d \n", stud->roll_no);
return 0;
}
Output:
Student name = Jane Doe
Student roll no = 30
Creating simple structure with a char pointer and accessing elements inside them
In this example, we shall create a structure variable inside main and allocate memory inside stack.
Below is a “student” structure that has 2 elements. A char pointer to hold name and int variable to hold roll number.
Then we create a structure variable inside main, so it will create memory inside stack.
Once you have created static memory for the structure variable, for the char pointer, only the memory for that pointer will be allocated.
Hence you need to explicitly allocate memory for the string to be held. This is done with the help of dynamic memory management.
Then as we have statically allocated memory, we can access the elements by dot “.” operators.
Example:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student_database
{
char *name;
int roll_no;
};
int main()
{
struct student_database stud;
stud.name = (char*) malloc(sizeof("www.ProdeveloperTutorial.com")+1);
strcpy(stud.name, "www.ProdeveloperTutorial.com");
stud.roll_no = 20;
printf("Student name = %s \n", stud.name);
printf("Student roll no = %d \n", stud.roll_no);
return 0;
}
Output:
Student name = www.ProdeveloperTutorial.com
Student roll no = 20
Creating simple structure with a char pointer and accessing elements inside them. Allocate memory dynamically.
Here also when we allocate memory dynamically for the structure, space is allocated for char pointer only.
So you need to allocate memory to accommodate the string dynamically.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student_database
{
char *name;
int roll_no;
};
int main()
{
struct student_database *stud = NULL;
stud = (struct student_database*) malloc(sizeof(struct student_database));
stud->roll_no = 30;
stud->name= (char *) malloc( sizeof("Jane Doe")+1);
strcpy(stud->name, "Jane Doe");
printf("Student name = %s \n", stud->name);
printf("Student roll no = %d \n", stud->roll_no);
return 0;
}
Output:
Student name = Jane Doe
Student roll no = 30
How to accessing elements of structure inside a structure:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student_detail
{
char name[40];
int roll_no;
};
struct student_database
{
struct student_detail detail;
int percentage;
};
int main()
{
// statically allocated memory
struct student_database student_database_1;
student_database_1.percentage = 80;
student_database_1.detail.roll_no = 30;
strcpy(student_database_1.detail.name, "Jane Doe");
printf("Student 1 percentage = %d\n", student_database_1.percentage);
printf("Student 1 roll_no = %d\n", student_database_1.detail.roll_no);
printf("Student 1 name = %s\n", student_database_1.detail.name);
struct student_database *student_database_2;
student_database_2 = (struct student_database*) malloc(sizeof(struct student_database));
student_database_2->percentage = 70;
student_database_2->detail.roll_no = 10;
strcpy(student_database_2->detail.name , "New John");
printf("Student 2 percentage = %d\n", student_database_2->percentage);
printf("Student 2 roll_no = %d\n", student_database_2->detail.roll_no);
printf("Student 2 name = %s\n", student_database_2->detail.name);
return 0;
}
Output:
Student 1 percentage = 80
Student 1 roll_no = 30
Student 1 name = Jane Doe
Student 2 percentage = 70
Student 2 roll_no = 10
Student 2 name = New John
Pass structure to a function by value
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student_database
{
char name[30];
int roll_no;
};
void display_data(struct student_database stud)
{
printf("Student name = %s \n", stud.name);
printf("Student roll no = %d \n", stud.roll_no);
}
int main()
{
struct student_database stud = {"jane", 2};
display_data(stud);
return 0;
}
Output:
Student name = jane
Student roll no = 2
Pass structure to a function by reference
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student_database
{
char name[30];
int roll_no;
};
void display_data(struct student_database *stud)
{
printf("Student name = %s \n", stud->name);
printf("Student roll no = %d \n", stud->roll_no);
}
int main()
{
struct student_database stud = {"jane", 2};
display_data(&stud);
return 0;
}
Output:
Student name = jane
Student roll no = 2
Return structure to a function
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student_database
{
char name[30];
int roll_no;
};
struct student_database* get_data()
{
struct student_database *stud = NULL;
stud = (struct student_database*) malloc(sizeof(struct student_database)); stud->roll_no = 30;
strcpy(stud->name, "Jane Doe");
return stud;
}
int main()
{
struct student_database *stud = NULL;
stud = get_data();
printf("Student name = %s \n", stud->name);
printf("Student roll no = %d \n", stud->roll_no);
return 0;
}
Output:
Student name = Jane Doe
Student roll no = 30