C - Passing structure to function

C Programming

Share

In this tutorial we will learn to pass structures to functions in C programming language.

In the Structures and Arrays tutorial we learned how to create array of structures. In this tutorial we will be using some of those concepts. So, feel free to recap the previous tutorial.

Lets get started...

To pass a structure to a function we have to properly declare the function parameter list.

In the following example we are creating a student structure.

struct student {
  char firstname[64];
  char lastname[64];
  char id[64];
  int score;
};

Now, lets say we want to create a displayDetail() function which takes the student structure variable as argument and prints the details.

For this we will have to first declare the displayDetail() function.

Syntax of a function declaration taking structure as argument

Following is the function declaration syntax to accept structure variable as argument.

returnType functionName(struct tagName argName);

Example:

void displayDetail(struct student std);

In the above code we are declaring a function named displayDetail. The return type of this function is set to void which means the function will return no value.

In the list of parameters we have std of struct student type. This means std is a variable of student structure. So, the function displayDetail can take any variable of type student structure as argument.

Passing structure variable to function

To pass a structure variable to a function all we have to do is write the name of the variable and it will pass a copy of the structure variable.

In the following example code we are passing stdArr[i] variable of type student structure to the displayDetail function.

displayDetail(stdArr[i]);

Write a program in C to take details of 3 students as input and display the result by passing the structure to a function

#include <stdio.h>

// creating a student structure template
struct student {
  char firstname[64];
  char lastname[64];
  char id[64];
  int score;
};
  
// function declaration
void displayDetail(struct student std);

int main(void) {
  
  // creating a student structure array variable
  struct student stdArr[3];
  
  // other variables
  int i;
  
  // taking user input
  for (i = 0; i < 3; i++) {
    printf("Enter detail of student #%d\n", (i+1));

    printf("Enter First Name: ");
    scanf("%s", stdArr[i].firstname);
  
    printf("Enter Last Name: ");
    scanf("%s", stdArr[i].lastname);
  
    printf("Enter ID: ");
    scanf("%s", stdArr[i].id);
  
    printf("Enter Score: ");
    scanf("%d", &stdArr[i].score);
  }
  
  // output
  for (i = 0; i < 3; i++) {
    printf("\nStudent #%d Detail:\n", (i+1));
    displayDetail(stdArr[i]);
  }
  
  return 0;
}

void displayDetail(struct student std) {
  printf("Firstname: %s\n", std.firstname);
  printf("Lastname: %s\n", std.lastname);
  printf("ID: %s\n", std.id);
  printf("Score: %d\n", std.score);
}

Output:

Enter detail of student #1
Enter First Name: Bruce 
Enter Last Name: Wayne
Enter ID: dc-01
Enter Score: 8
Enter detail of student #2
Enter First Name: Peter
Enter Last Name: Parker
Enter ID: mc-01
Enter Score: 9
Enter detail of student #3
Enter First Name: Tony
Enter Last Name: Stark
Enter ID: mc-02
Enter Score: 7

Student #1 Detail:
Firstname: Bruce
Lastname: Wayne
ID: dc-01
Score: 8

Student #2 Detail:
Firstname: Peter
Lastname: Parker
ID: mc-01
Score: 9

Student #3 Detail:
Firstname: Tony
Lastname: Stark
ID: mc-02
Score: 7
Share