¤ Home » Programming » C Tutorial » How to pass Structure as a parameter to a function in C?

How to pass Structure as a parameter to a function in C?

An entire structure can be passed to a function as its parameter so that the function can process an entire record and return some result. For instance, you could pass a student record to a function which in turn would compute the average of marks obtained in all subjects in all the semesters and return the aggregate grade.

A structure can be transferred to a function either using call by value or call by reference scheme.

Remember that C only implements call by value scheme of parameter transmission. Call by reference is indirectly implemented by passing address of variable.

A function can also return an instance of a structure using the return statement. Though all compilers may not support this. Hence, you should avoid returning a complete structure from a function. If the function has to alter the data stored in the structure, it would be better to pass the address of the structure variable. The function can then work on the members of the passed structure using pointers.


A typical usage of passing structure to a function is illustrated in the below example.

#include <stdio.h>

struct name {
   char first_name[21];
   char middle_name[21];
   char last_name[21];
};

typedef struct {
   int    enrolment_no;
   struct name stud_name;
   char   course_name[41];
   float  module_marks[4];
   char   grade;
} student;

main()
{
   void read_student(student *);

   static student a = {1234, "Sunidhi", "Narayan", "Mishra", "Arts", 45, 78, 98, 67, 'A'};	

   /* Print initialized student data */
   printf("\nEnrollment No: %d\nFirst Name: %s\nMiddle Name : %s\nLast Name: %s\nCourse Name: %s\nGrade: %c\n", 
      a.enrolment_no, a.stud_name.first_name, a.stud_name.middle_name, a.stud_name.last_name, a.course_name, a.grade);

   /*Function call to read student data inputted by user */ 
   read_student(&a);

   /* Print user provided student data */
   printf("\nEnrollment No: %d\nFirst Name: %s\nMiddle Name : %s\nLast Name: %s\nCourse Name: %s\nGrade: %c\n", 
      a.enrolment_no, a.stud_name.first_name, a.stud_name.middle_name, a.stud_name.last_name, a.course_name, a.grade);
}

void read_student(student *a) 
{
   printf("\nEnrollment No.: "); scanf("%d", &a->enrolment_no); fflush(stdin);
   printf("\nFirst Name: ");     gets(a->stud_name.first_name);
   printf("\nMiddle Name: ");    gets(a->stud_name.middle_name);
   printf("\nLast Name: ");      gets(a->stud_name.last_name);
   printf("\nCourse: ");         gets(a->course_name);
	
   printf("\nMarks (comma separated 4 marks): ");
   scanf("%f, %f, %f, %f", &a->module_marks[0], &a->module_marks[1], &a->module_marks[2], &a->module_marks[3]);
   fflush(stdin);

   printf("\nGrade: "); scanf("%c", &a->grade); fflush(stdin);
   return;
}


A structure can be transmitted to another function using a pointer which as we have seen above implements the call by reference scheme. The address of the structure transmitted by the calling function is received in the called function in a pointer to the same type of structure. In the called function, the individual structure members are accessed by the pointer variable using the notation, ps->name or (*ps).name.

Let us look at another example.

#include <stdio.h>

typedef struct {
	float real;
	float imag;
} complex;
 
complex add_complex(complex, complex, complex *);
void read_complex(complex *);
void write_complex(complex);

main() 
{ 
	complex a, b, c;
	read_complex(&a);
	read_complex(&b);
	add_complex(a, b, &c);
	write_complex(c);
}

complex add_complex(complex x, complex y, complex *tot)
{
	tot->real = x.real + y.real;
	tot->imag = x.imag + y.imag;
	return;
}

void read_complex(complex *x)
{
	printf("\nEnter the real and imaginary components of the complex number: ");
	scanf("%f %f", &(x->real), &(x->imag));
}

void write_complex(complex x)
{
	char c[3] = "+j";
	if(x.imag < 0)
	{
		c[0] = '-';
		x.imag = -x.imag;
	}

	printf("Resultant Complex Number = %.2f%s%.2f", x.real, c, x.imag);
}



Exercises

For each of following C programs, describe the output that will be generated.

#include<stdio.h>
typedef struct {
	char *x;
	char *y;
	char *z;
} planets;

main()
{
	void f(planets sample);
	static planets sample = {"earth", "mars", "jupiter"};

	printf("%s %s %s \n", sample.x, sample.y, sample.z);
	f(sample);
	printf("%s %s %s \n", sample.x, sample.y, sample.z);
}

void f(planets sample)
{
	sample.x = "mercury";
	sample.y = "venus";
	sample.z = "saturn";
	printf("%s %s %s \n", sample.x, sample.y, sample.z);
	return;
}


#include <stdio.h>

typedef struct{
	char *x;
	char *y;
	char *z;
} planets;

main()
{
	void f(planets *);
	static  planets sample = {"earth", "mars", "Jupiter"};
	printf("%s %s %s\n", sample.x, sample.y, sample.z);
	f(&sample);
	printf("%s %s %s\n", sample.x, sample.y, sample.z);
}

void f(planets *sample)
{
	sample->x = "mercury";
	sample->y = "venus";
	sample->z = "saturn";
	printf("%s %s %s\n", sample->x, sample->y, sample->z);
	return;
}


#include <stdio.h>

typedef union {
	int   a;
	float b;
} sample;

main()
{
	sample u;
	void f(sample *);
	
	u.a = 20;
	u.b = 1.6;
	printf("%d, %f\n", u.a, u.b);
	
	f(&u);
	printf("%d %f\n", u.a, u.b);
}

void f(sample *x)
{
	x->a = 500;
	printf("%d %f\n", x->a, x->b);
	return;
}

In the above code, ask yourself - why is arbitrary value printed for one of the elements of the union.



Share:




comments powered by Disqus


Web Service Offers

Third Level Dot In Registration
599 199 (66.78% off)
per Year

.IN Registration
749 199 (73.43% off)
per Year

Ecommerce on the Cloud

An online store hosted on the Cloud. Sell unlimited products. Integrated business functions. Well-suited for high traffic website.

39,999 29,999 (25% off)

Ecommerce Premium

An e-commerce website well-suited for companies having a large portfolio of products. Sell unlimited products. Manage both offline and online sales from one system.

40,000 25,000 (37.5% off)

Ecommerce Business

Launch your online business. Sell up to 5000 products. Collect payment via multiple online and offline payment methods.

35,000 25,000 (28.57% off)

Ecommerce Pro

Get a Mobile friendly online store front. Sell up to 1000 products.

35,000 25,000 (28.57% off)

Ecommerce Starter

Start selling your products online. Scale up as your business grows.

35,000 25,000 (28.57% off)

Website on the Cloud

Custom built, responsive corporate website hosted on the cloud. Mirrored on 3 servers. Includes visitor signup with CRM functions/

19,999 9,999 (50% off)

Website Corporate

Custom built, responsive corporate website with CRM. Our most popular website plan.

19,000 9,000 (52.63% off)

Website Business - 1000 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

18,000 8,000 (55.56% off)

Website Business - 500 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,500 7,500 (57.14% off)

Website Business - 100 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,000 7,000 (58.82% off)

Website Business - 50 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,500 6,500 (60.61% off)

Website Business - 25 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,000 6,000 (62.5% off)

Website Starter - 5 Pages

Get started with your own website at most affordable cost.

15,000 5,000 (66.67% off)


Looking to build a website?
Launch a 5 page website in 1 day at only Rs.499/year. Hosting included.





About the Author

Rajeev Kumar
CEO, Computer Solutions
Jamshedpur, India

Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of XLRI, industry professionals, and govt. officials.

Rajeev has founded Computer Solutions & WebServicesWorldwide.com, and has hands-on experience of building variety of web applications and portals, that include - SAAS based ERP & e-commerce systems, independent B2B, B2C, Matrimonial & Job portals, and many more.



Copyright © How2Lab.com. All rights reserved.

Refer a friend | Sitemap | Disclaimer | Privacy Policy