Structure to Function in C Programming Language

Rumman Ansari   Software Engineer   2023-08-15   9725 Share
☰ Table of Contents

Table of Content:


Passing Structures to Functions

This section discusses passing structures and their members to functions. A structure can be passed to any function from the main function or from any subfunction. Structure definition will be available within the function only. It won’t be available to other functions unless it is passed to those functions by value or by address(reference). Else, we have to declare structure variable as a global variable. That means structure variable should be declared outside the main function. So, this structure will be visible to all the functions in a C program.

Passing Structure Members to Functions

When you pass a member of a structure to a function, you are passing the value of that member to the function. It is irrelevant that the value is obtained from a member of a structure. For example, consider this structure:

 struct structName
{
char p;
int q;
float r;
char s[10];
} record1;

Here are examples of each member being passed to a function:

func(record1.p); /* passes character value of p */
func2(record1.q); /* passes integer value of q */
func3(record1.r); /* passes float value of r */
func4(record1.s); /* passes address of string s */
func(record1.s[2]); /* passes character value of s[2] */
 

Example Program


#include <stdio.h>
/* Define a structure type. */
struct struct_type {
int p;
char q;
} ;

void f1(int item);
int main(void)
{
	struct struct_type record1;
	record1.p = 10;
	f1(record1.p); /* passes character value of p */
	return 0;
}
void f1(int item)
{
	printf("%d \n",item);
}

Output

10
Press any key to continue . . .

In each case, it is the value of a specific element that is passed to the function. It does not matter that the element is part of a larger unit.

If you wish to pass the address of an individual structure member, put the & operator before the structure name. For example, to pass the address of the members of the structure record1, write

func(&record1.p); /* passes address of character p */
func2(&record1.q); /* passes address of integer q */
func3(&record1.r); /* passes address of float r */
func4(record1.s); /* passes address of string s */
func(&record1.s[2]); /* passes address of character s[2] */
 

Example Program


#include <stdio.h>
/* Define a structure type. */
struct struct_type {
int p;
char q;
} ;

void f1(int *item);
int main(void)
{
	struct struct_type record1;
	record1.p = 10;
	f1(&record1.p); /* passes address of character p */
	return 0;
}
void f1(int *item)
{
	printf("%d \n",*item);
}

Output

10
Press any key to continue . . .

Assign structure to a function by value


#include <stdio.h>
#include <string.h>

struct student 
{
 int id;
 char name[20];
 float percentage;
};

void func(struct student stu1);

int main() 
{
 struct student stu1;

 stu1.id=21;
 strcpy(stu1.name, "Rambo");
 stu1.percentage = 96.5;

 func(stu1);
 return 0;
}

void func(struct student stu1)
{
 printf(" Id is: %d \n", stu1.id);
 printf(" Name is: %s \n", stu1.name);
 printf(" Percentage is: %f \n", stu1.percentage);
}
 Id is: 21
 Name is: Rambo
 Percentage is: 96.500000
Press any key to continue . . .

Passing structure to a function by address(reference)

In this program, the whole structure is passed to another function by address. It means only the address of the structure is passed to another function. The whole structure is not passed to another function with all members and their values. So, this structure can be accessed from called function by its address.


 #include <stdio.h>
#include <string.h>
 
struct student 
{
           int id;
           char name[20];
           float percentage;
};
 
void func(struct student *stu1);
 
int main() 
{
	struct student stu1;
 
	stu1.id=21;
	strcpy(stu1.name, "Rambo");
	stu1.percentage = 96.5;
 
	func(&stu1);
	return 0;
}
 
void func(struct student *stu1)
{
	printf(" Id is: %d \n", stu1->id);
	printf(" Name is: %s \n", stu1->name);
	printf(" Percentage is: %f \n", stu1->percentage);
}
  Id is: 21
 Name is: Rambo
 Percentage is: 96.500000
Press any key to continue . . .

No need to pass a structure – Declare structure variable as global

Structure variables also can be declared as global variables as we declare other variables in C. So, When a structure variable is declared as global, then it is visible to all the functions in a program. In this scenario, we don't need to pass the structure to any function separately.

Example Program


 #include <stdio.h>
#include <string.h>
 
struct student 
{
            int id;
            char name[20];
            float percentage;
};
struct student stu1; // Global declaration of structure
 
void structurefunction();
 
int main() 
{
            stu1.id=21;
            strcpy(stu1.name, "Rambo");
            stu1.percentage = 96.5;
 
            structurefunction();
            return 0;
}
 
void structurefunction()
{
            printf(" Id is: %d \n", stu1.id);
            printf(" Name is: %s \n", stu1.name);
            printf(" Percentage is: %f \n", stu1.percentage);
}
  Id is: 21
 Name is: Rambo
 Percentage is: 96.500000
Press any key to continue . . .

Passing Entire Structures to Functions

When a structure is used as an argument to a function, the entire structure is passed using the normal call-by-value method. Of course, this means that any changes made to the contents of the parameter inside the function do not affect the structure passed as the argument.

When using a structure as a parameter, remember that the type of the argument must match the type of the parameter. For example, in the following program both the argument record1 and the parameter parameter are declared as the same type of structure.

Example Program


#include <stdio.h>
/* Define a structure type. */
struct struct_type {
	int a, b;
	char ch;
} ;
void f1(struct struct_type record1);


int main(void)
{
	struct struct_type record1;
	record1.a = 1070;
	
	f1(record1);
	
return 0;
}
void f1(struct struct_type parameter)
{
	printf("%d \n", parameter.a);
}
10
Press any key to continue . . .

As this program illustrates, if you will be declaring parameters that are structures, you must make the declaration of the structure type global so that all parts of your program can use it. For example, had struct_type been declared inside main( ), it would not have been visible to f1( ).