Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction  
2.
Passing Structure Members as Arguments to Function
3.
Passing Structure Variable as Argument to Function
4.
Passing Structure Pointers as Argument to Function
5.
Returning a Structure Variable from a Function
6.
Returning a Structure Pointer from a Function
7.
Frequently asked question
8.
Key takeaways
Last Updated: Mar 27, 2024

Structures and Functions

Author Shivam Verma
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction  

The C Programming language allows us to pass the structures as arguments to a function in different ways. We can pass individual members, structure variables, or structure pointers to the function. Similarly, a function can return either an individual member or whole structure variable or pointer to the structure.

Also Read About, Sum of Digits in C and C Static Function.

Passing Structure Members as Arguments to Function

We can pass individual structure members as arguments to functions like any other ordinary variable.

Here is a program to demonstrate how to pass structure members as arguments to the function.

Code 

#include<stdio.h>
struct student
{
    char name[20];
    int rollno;
    float marks;
};
void display_struct(char name[], int rollno, float marks)
{
    printf("Name of the student: %s\n", name);
    printf("Roll number of the student: %d\n", rollno);
    printf("Percentage marks of the student: %.2f\n", marks);
    printf("\n");
}
int main()
{
    struct student stu1= {"Deepak", 12, 82.4};
    display_struct(stu1.name, stu1.rollno, stu1.marks);
    return 0;
}

Output

Name of the student: Deepak
Roll number of the student: 12
Percentage marks of the student: 82.40

In the above program, We have passed members of variable stu1 to the function display_struct(). The names of the formal arguments can be similar to the names of the members.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

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

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

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

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

  1. 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.
     
  2. 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.
     
  3. 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. 

Previous article
Introduction to Structure
Next article
Typedef in c
Live masterclass