You are on page 1of 14

C program to pass an array of structure

to a user-defined function
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,

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

Ponter to structure

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.
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++;

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

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.

Structure Pointer
The structure pointer points to the address of a memory block where the Structure
is being stored. Like a pointer that tells the address of another variable of any data
type (int, char, float) in memory. And here, we use a structure pointer which tells the
address of a structure in memory by pointing pointer variable ptr to the structure
variable.
Declare a Structure Pointer
The declaration of a structure pointer is similar to the declaration of the structure
variable. So, we can declare the structure pointer and variable inside and outside of
the main() function. To declare a pointer variable in C, we use the asterisk (*) symbol
before the variable's name.

1. struct structure_name *ptr;  

After defining the structure pointer, we need to initialize it, as the code is shown:

Initialization of the Structure Pointer


ptr = &structure_variable;

We can also initialize a Structure Pointer directly during the declaration of a pointer.

1. struct structure_name *ptr = &structure_variable;  

As we can see, a pointer ptr is pointing to the address structure_variable of the


Structure.

Access Structure member using pointer:


There are two ways to access the member of the structure using Structure pointer:

1. Using ( * ) asterisk or indirection operator and dot ( . ) operator.


2. Using arrow ( -> ) operator or membership operator.

Program to access the structure member using structure


pointer and the dot operator
Let's consider an example to create a Subject structure and access its members using
a structure pointer that points to the address of the Subject variable in C.

Pointer.c

#include <stdio.h>  
  
// create a structure Subject using the struct keyword  
struct Subject  
{  
    // declare the member of the Course structure  
    char sub_name[30];  
    int sub_id;  
    char sub_duration[50];  
    char sub_type[50];  
};  
  
int main()  
{  
    struct Subject sub; // declare the Subject variable  
    struct Subject *ptr; // create a pointer variable (*ptr)   
    ptr = ⊂ /* ptr variable pointing to the address of the structure variable sub 
*/  
      
    strcpy (sub.sub_name, " Computer Science");  
    sub.sub_id = 1201;  
    strcpy (sub.sub_duration, "6 Months");  
    strcpy (sub.sub_type, " Multiple Choice Question");  
  
    // print the details of the Subject;  
    printf (" Subject Name: %s\t ", (*ptr).sub_name);  
            printf (" \n Subject Id: %d\t ", (*ptr).sub_id);  
        printf (" \n Duration of the Subject: %s\t ", (*ptr).sub_duration);  
           printf (" \n Type of the Subject: %s\t ", (*ptr).sub_type);  
  
    return 0;  
      
}  

You might also like