C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Passing structure pointer to function

C Programming

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.

MemberData TypeSize
idchar15 bytes
firstnamechar64 bytes
lastnamechar64 bytes
pointsfloat4 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.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT