C Structure and Programming

How to pass a structure to a function in C programming?

In this article, you'll find relevant examples to pass structures as an argument to a function, and use them in your program.
C Programming Structure and Function
In C, structure can be passed to functions by two methods:
  1. Passing by value (passing actual value as argument)
  2. Passing by reference (passing address of an argument)

Passing structure by value

A structure variable can be passed to the function as an argument as a normal variable.
If structure is passed by value, changes made to the structure variable inside the function definition does not reflect in the originally passed structure variable.
C program to create a structure student, containing name and roll and display the information.
#include <stdio.h>
struct student
{
    char name[50];
    int roll;
};

void display(struct student stu);
// function prototype should be below to the structure declaration otherwise compiler shows error

int main()
{
    struct student stud;

    printf("Enter student's name: ");
    scanf ("%[^\n]%*c", stud.name);

    printf("Enter roll number:");
    scanf("%d", &stud.roll);
    
    display(stud);   // passing structure variable stud as argument
    
    return 0;
}
void display(struct student stu){
  printf("Output\nName: %s",stu.name);
  printf("\nRoll: %d",stu.roll);
}
Output
Enter student's name: Kevin Amla
Enter roll number: 149
Output
Name: Kevin Amla
Roll: 149

Passing structure by reference

The memory address of a structure variable is passed to function while passing it by reference.
If structure is passed by reference, changes made to the structure variable inside function definition reflects in the originally passed structure variable.
C program to add two distances (feet-inch system) and display the result without the return statement.
#include <stdio.h>
struct distance
{
    int feet;
    float inch;
};
void add(struct distance d1,struct distance d2, struct distance *d3); 

int main()
{
    struct distance dist1, dist2, dist3;

    printf("First distance\n");
    printf("Enter feet: ");
    scanf("%d", &dist1.feet);
    printf("Enter inch: ");
    scanf("%f", &dist1.inch);

    printf("Second distance\n");
    printf("Enter feet: ");
    scanf("%d", &dist2.feet);
    printf("Enter inch: ");
    scanf("%f", &dist2.inch);

    add(dist1, dist2, &dist3); 

    //passing structure variables dist1 and dist2 by value whereas passing structure variable dist3 by reference
    printf("\nSum of distances = %d\'-%.1f\"", dist3.feet, dist3.inch);

    return 0;
}
void add(struct distance d1,struct distance d2, struct distance *d3) 
{
     //Adding distances d1 and d2 and storing it in d3
     d3->feet = d1.feet + d2.feet; 
     d3->inch = d1.inch + d2.inch;

     if (d3->inch >= 12) {     /* if inch is greater or equal to 12, converting it to feet. */
         d3->inch -= 12;
         ++d3->feet;
    }
}
Output
First distance
Enter feet: 12
Enter inch: 6.8
Second distance
Enter feet: 5
Enter inch: 7.5

Sum of distances = 18'-2.3"
In this program, structure variables dist1 and dist2 are passed by value to the addfunction (because value of dist1 and dist2 does not need to be displayed in main function).
But, dist3 is passed by reference ,i.e, address of dist3 (&dist3) is passed as an argument.
Due to this, the structure pointer variable d3 inside the add function points to the address of dist3 from the calling main function. So, any change made to the d3 variable is seen in dist3 variable in main function.
As a result, the correct sum is displayed in the output.
PREVIOUS                            NEXT

Comments

Popular

C Programming Enumeration

C switch...case Statement

List of all Keywords in C Language

C Programming Structure

While and Do...While Loop

C Preprocessor and Macros

C Operators