C program to pass an array of structure to a user-defined function

In this C program, we are going to learn how to pass an array of structure to a user define function? Here, we are an example, where we are passing a structure to a function in C.
Submitted by IncludeHelp, on March 22, 2018

Given an array of structure and we have to pass it to the function in C.

structure declaration is:

    struct exam
    {
        int roll;
        int marks;
        char name[20];
    };

Here,

  • exam is the structure name
  • roll, marks and name are the members of the structure/variable of the structure

Here is the function that we are using in the program,

void structfun(struct exam obj1);

Here,

  • void is the returns type of the function i.e. function will not return any value.
  • structfun is the name of the function.
  • struct exam obj1 is the object of exam structure - while declaring a function we must have to use struct keyword with the structure name.

Structure object/variable declaration is:

    struct exam obj[2];

obj is an array of structure for 2 structures.

Assigning values to the structure variables (array of structure variables) inside the main()

    // assign values using the object 1
    obj[0].marks = 35;
    obj[0].roll = 10;
    strcpy(obj[0].name , "Arjun kapoor");
	
    // assign values using the object 1
    obj[1].marks = 75;
    obj[1].roll = 11;
    strcpy(obj[1].name , "Balram chauhan");

Function calling statement,

structfun(obj);

Here, obj is an array of structure.

Program to pass an array of structures to a function in C

/*  
C program to pass an arrays of structures
to a function
*/

#include <stdio.h>

// Declare a global structure since we need to pass 
// it to a function 
struct exam
{
    int roll;
    int marks;
    char name[20];
};

// array of structure object
struct exam obj[2];

 // declaration of the function
void structfun(struct exam *obj);

// function to print structure elements switch
// two different objects
void structfun(struct exam *obj)
{
    //Values using the object 1
    printf("\nName is : %s",obj[0].name);
    printf("\nRoll No. is : %d",obj[0].roll);
    printf("\nMarks are : %d",obj[0].marks);
    
    printf("\n");
    
    // Values using the object 2
    printf("\nName is : %s",obj[1].name);
    printf("\nRoll No. is : %d",obj[1].roll);
    printf("\nMarks are : %d",obj[1].marks);
}

// main function
int main()
{
    // assign values using the object 1
    obj[0].marks = 35;
    obj[0].roll = 10;
    strcpy(obj[0].name , "Arjun kapoor");
	
    // assign values using the object 1
    obj[1].marks = 75;
    obj[1].roll = 11;
    strcpy(obj[1].name , "Balram chauhan");
    
    // Passing structure to Function
    structfun(obj);
	
    return 0;
}

Output

Name is : Arjun kapoor
Roll No. is : 10
Marks are : 35

Name is : Balram chauhan
Roll No. is : 11
Marks are : 75

C User-defined Functions Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.