Passing Structure Variable as Argument to Function
If a structure contains two to three members, we can easily pass them to function, but it is a tiresome and error-prone process when there are many members. So in such cases, instead of passing members individually, we can pass the whole structure as an argument.
Here is a program to demonstrate how we can pass structure variables as an argument to the function.
Code
#include<stdio.h>
struct student
{
char name[20];
int rollno;
float marks;
};
void display_struct(struct student stu1)
{
printf("Name of the student: %s\n", stu1.name);
printf("Roll number of the student: %d\n", stu1.rollno);
printf("Percentage marks of the student: %.2f\n", stu1.marks);
printf("\n");
}
int main()
{
struct student stu1= {"Deepak", 12, 82.4};
display_struct(stu1);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Name of the student: Deepak
Roll number of the student: 12
Percentage marks of the student: 82.40
In the above program, it is necessary to define the structure template globally because it is used by both main() and display_struct() functions to declare variables. When we send a structure variable as an argument to a function, a copy of the whole structure is made inside the called function, and all the work is done on that copy.
You can also read about the dynamic arrays in c, and Tribonacci Series
Passing Structure Pointers as Argument to Function
If the size of a structure is very large, then the Call by value method is inefficient to pass the whole structure to the function. C provides the efficient method known as Call by reference to pass large structures to functions. In this method, we send the address of the structure to the function.
Here is a program to demonstrate how to pass structure pointers as arguments to a function.
Code
#include<stdio.h>
struct student
{
char name[20];
int rollno;
float marks;
};
void increment_mark(struct student *stu_ptr)
{
stu_ptr->marks++;
}
void display_struct(struct student *stu_ptr)
{
printf("Name: %s", stu_ptr->name);
printf(" Roll number: %d", stu_ptr->rollno);
printf(" Percentage marks: %.2f", stu_ptr->marks);
printf("\n");
}
int main()
{
struct student stu1= {"Deepak", 12, 82.4};
display_struct(&stu1);
increment_mark(&stu1);
display_struct(&stu1);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Name: Deepak Roll number: 12 Percentage marks: 82.40
Name: Deepak Roll number: 12 Percentage marks: 83.40
In the above program, We can access the structure variable members inside the calling function using the arrow operator. We can also increment the student's marks inside the increment_mark() function as shown in the program.
Also see, Short int in C Programming
Returning a Structure Variable from a Function
The structure variable can be returned from the function as any other variable. To return a structure variable from a function, we have to specify the appropriate return type in the function definition and declaration.
Here is a program to demonstrate how we can return a structure variable from a function.
Code
#include<stdio.h>
struct student
{
char name[20];
int rollno;
float marks;
};
struct student increment_mark(struct student stu)
{
stu.marks=stu.marks+5;
return stu;
}
void display_struct(struct student stu1)
{
printf("Name: %s", stu1.name);
printf(" Roll number: %d", stu1.rollno);
printf(" Percentage marks: %.2f", stu1.marks);
printf("\n");
}
int main()
{
struct student stu1= {"Deepak", 12, 82.4};
display_struct(stu1);
stu1=increment_mark(stu1);
display_struct(stu1);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Name: Deepak Roll number: 12 Percentage marks: 82.40
Name: Deepak Roll number: 12 Percentage marks: 87.40
In the above program, We incremented the marks of the student using the increment_mark() function and then returned the incremented marks.
Returning a Structure Pointer from a Function
Pointers to structure can also be returned from functions. To return a structure pointer from a function, we have to specify the appropriate return type in the function definition and declaration.
Here is a program to demonstrate how we can return structure pointers from a function.
Code
#include<stdio.h>
struct student
{
char name[20];
int rollno;
float marks;
};
struct student *increment_mark(struct student *stu_ptr)
{
stu_ptr->marks=stu_ptr->marks+3;
return stu_ptr;
}
void display_struct(struct student *stu1_ptr)
{
printf("Name of the student: %s\n", stu1_ptr->name);
printf("Roll number of the student: %d\n", stu1_ptr->rollno);
printf("Percentage marks of the student: %.2f\n", stu1_ptr->marks);
printf("\n");
}
int main()
{
struct student stu1= {"Deepak", 12, 82.4};
struct student *stuptr=increment_mark(&stu1);
display_struct(stuptr);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Name of the student: Deepak
Roll number of the student: 12
Percentage marks of the student: 85.40
In the above program, The increment_mark() function accepts an argument of type pointer to struct student and returns and a pointer of type struct student.
Frequently asked question
-
Why do we need an arrow operator(->) with structure pointers instead of a dot(.) operator?
Ans: In C language, Structure pointers store the address of a structure variable, so before accessing the structure members, we first need to dereference the operator; that's why we need to use the arrow operator instead of the dot operator.
-
Can we define functions in structs in the C programming language?
Ans: No, we can not define functions in structs in the C programming language.
-
What is the difference between a structure and a function in C language?
Ans: Function is a set of instructions that can be executed to obtain results and perform tasks.
The structure is a container that stores more than one heterogeneous (more than one type of data) data, which can be accessed/referenced by one variable name/token.
Key takeaways
In this blog, We learned how we could pass structure member, structure variable, and structure pointer to a function. This blog also discussed how we could return structure variable and structure pointer from a function.
This blog is over, but the thirst for learning is not over yet. To understand the difference between classes and structure, click here. You can use our practice platform Coding Ninjas Studio to practice various DSA questions asked in many interviews.
To know about Difference Between Compiler and Interpreter and Assembler click here.