How to pass Structure as a parameter to a function in C? (2024)

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:

How to pass Structure as a parameter to a function in C? (2024)
Top Articles
Unveiling the Mystery: Why Can’t I Manifest Money?
21.120 Historical Vehicle and Horseless Carriage License Plates (VC §5004) - California DMV
Stretchmark Camouflage Highland Park
Coverage of the introduction of the Water (Special Measures) Bill
Cash4Life Maryland Winning Numbers
Undergraduate Programs | Webster Vienna
Vocabulario A Level 2 Pp 36 40 Answers Key
Rls Elizabeth Nj
Corporate Homepage | Publix Super Markets
Craigslist Chautauqua Ny
Programmieren (kinder)leicht gemacht – mit Scratch! - fobizz
Help with Choosing Parts
Clarksburg Wv Craigslist Personals
Nwi Arrests Lake County
Dit is hoe de 130 nieuwe dubbele -deckers -treinen voor het land eruit zien
Letter F Logos - 178+ Best Letter F Logo Ideas. Free Letter F Logo Maker. | 99designs
Sam's Club La Habra Gas Prices
Q33 Bus Schedule Pdf
Vipleaguenba
Band Of Loyalty 5E
Is A Daytona Faster Than A Scat Pack
Poe Str Stacking
Vegito Clothes Xenoverse 2
C&T Wok Menu - Morrisville, NC Restaurant
Ltg Speech Copy Paste
Dei Ebill
Impact-Messung für bessere Ergebnisse « impact investing magazin
Snohomish Hairmasters
CohhCarnage - Twitch Streamer Profile & Bio - TopTwitchStreamers
His Only Son Showtimes Near Marquee Cinemas - Wakefield 12
Myaci Benefits Albertsons
Elijah Streams Videos
A Plus Nails Stewartville Mn
Inmate Search Disclaimer – Sheriff
Homewatch Caregivers Salary
Gina's Pizza Port Charlotte Fl
Of An Age Showtimes Near Alamo Drafthouse Sloans Lake
Appraisalport Com Dashboard /# Orders
2024 Ford Bronco Sport for sale - McDonough, GA - craigslist
Body Surface Area (BSA) Calculator
Mvnt Merchant Services
Daly City Building Division
The All-New MyUMobile App - Support | U Mobile
Vons Credit Union Routing Number
Lonely Wife Dating Club בקורות וחוות דעת משתמשים 2021
Courses In Touch
Mathews Vertix Mod Chart
CPM Homework Help
Unpleasant Realities Nyt
Uno Grade Scale
Secondary Math 2 Module 3 Answers
Ff14 Palebloom Kudzu Cloth
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 6354

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.