C - Passing structure pointer to function

C Programming

Share

In this tutorial we will learn to pass structure pointer to function in C programming language.

We learned about how to pass structure to a function in one of the earlier tutorial. So, we will be using that idea to pass structure pointer to a function.

Create a structure

In the following example are are creating a student structure.

// student structure
struct student {
  char id[15];
  char firstname[64];
  char lastname[64];
  float points;
};

Function declaration to accept structure pointer

Following is the syntax of the function declaration that accepts structure pointer.

returnType functionName(struct tagName *);

returnType is the return type of the function functionName. If the function is not returning anything then set it to void. The function takes structure tagName pointer.

In the following example we are creating two function declarations that takes address of student structure.

void getDetail(struct student *);
void displayDetail(struct student *);

Creating an array of structure variable

We will now create an array of student structure variable by writing the following code.

// student structure variable
struct student std[3];

So, we have created an array of student structure variable of size 3 to store the detail of three students.

We can represent the std array variable as follows.



Now we will write the complete code that will help us to get students data and then display them.

Complete code

#include <stdio.h>
    
// student structure
struct student {
  char id[15];
  char firstname[64];
  char lastname[64];
  float points;
};

// function declaration
void getDetail(struct student *);
void displayDetail(struct student *);

int main(void) {
  
  // student structure variable
  struct student std[3];
  
  // get student detail
  getDetail(std);
  
  // display student detail
  displayDetail(std);
  
  return 0;
}

// function definition


void getDetail(struct student *ptr) {

  int i;

  for (i = 0; i < 3; i++) {
    printf("Enter detail of student #%d\n", (i + 1));
    printf("Enter ID: ");
    scanf("%s", ptr->id);
    printf("Enter first name: ");
    scanf("%s", ptr->firstname);
    printf("Enter last name: ");
    scanf("%s", ptr->lastname);
    printf("Enter Points: ");
    scanf("%f", &ptr->points);
    
    // update pointer to point at next element
    // of the array std
    ptr++;
  }

}

void displayDetail(struct student *ptr) {

  int i;

  for (i = 0; i < 3; i++) {
    printf("\nDetail of student #%d\n", (i + 1));
    
    // display result via ptr variable
    printf("\nResult via ptr\n");
    printf("ID: %s\n", ptr->id);
    printf("First Name: %s\n", ptr->firstname);
    printf("Last Name: %s\n", ptr->lastname);
    printf("Points: %f\n", ptr->points);
    
    // update pointer to point at next element
    // of the array std
    ptr++;
  }

}

Output:

Enter detail of student #1
Enter ID: s01
Enter first name: Yusuf 
Enter last name: Shakeel
Enter Points: 8
Enter detail of student #2
Enter ID: s02
Enter first name: John
Enter last name: Doe
Enter Points: 7
Enter detail of student #3
Enter ID: s03
Enter first name: Jane
Enter last name: Doe
Enter Points: 9

Detail of student #1

Result via ptr
ID: s01
First Name: Yusuf
Last Name: Shakeel
Points: 8.000000

Detail of student #2

Result via ptr
ID: s02
First Name: John
Last Name: Doe
Points: 7.000000

Detail of student #3

Result via ptr
ID: s03
First Name: Jane
Last Name: Doe
Points: 9.000000

We can represent the std structure array variable and the function parameter as follows.

Note! In the given image we are showing displayDetail() function. The getDetail() function will also look similar.



Points to note!

Each std element is taking 147 bytes of memory which we can represent as follows.

Member Data Type Size
id char 15 bytes
firstname char 64 bytes
lastname char 64 bytes
points float 4 bytes

When we pass the std variable as argument to the getDetail() and displayDetail() function, we are actually passing the address of the variable i.e., the starting address of std.

The starting address of std is then assigned to the ptr variable and we work with it to access the members of the student structure.

Share

Recently Updated