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
Ludociel
How to Spot Phishing, Vishing and Smishing Scams - Nationwide
Craigslist St. Paul
Windcrest Little League Baseball
Jailbase Orlando
Black Gelato Strain Allbud
Chelsea player who left on a free is now worth more than Palmer & Caicedo
Practical Magic 123Movies
How to know if a financial advisor is good?
Phenix Food Locker Weekly Ad
Which aspects are important in sales |#1 Prospection
Osrs Blessed Axe
Theycallmemissblue
N2O4 Lewis Structure & Characteristics (13 Complete Facts)
Craftology East Peoria Il
Highland Park, Los Angeles, Neighborhood Guide
Images of CGC-graded Comic Books Now Available Using the CGC Certification Verification Tool
Aucklanders brace for gales, hail, cold temperatures, possible blackouts; snow falls in Chch
Craigslist West Valley
Ge-Tracker Bond
Robin D Bullock Family Photos
Best Mechanics Near You - Brake Masters Auto Repair Shops
Phoebus uses last-second touchdown to stun Salem for Class 4 football title
Dragger Games For The Brain
Bjerrum difference plots - Big Chemical Encyclopedia
Www.dunkinbaskinrunsonyou.con
Mybiglots Net Associates
Gotcha Rva 2022
Cookie Clicker Advanced Method Unblocked
TMO GRC Fortworth TX | T-Mobile Community
Possum Exam Fallout 76
Deepwoken: Best Attunement Tier List - Item Level Gaming
United E Gift Card
Ripsi Terzian Instagram
How does paysafecard work? The only guide you need
Hingham Police Scanner Wicked Local
Felix Mallard Lpsg
Kornerstone Funeral Tulia
Columbia Ms Buy Sell Trade
PruittHealth hiring Certified Nursing Assistant - Third Shift in Augusta, GA | LinkedIn
Callie Gullickson Eye Patches
Brandon Spikes Career Earnings
Locate phone number
Bekah Birdsall Measurements
Pain Out Maxx Kratom
Windy Bee Favor
Erica Mena Net Worth Forbes
18443168434
Dumb Money Showtimes Near Regal Stonecrest At Piper Glen
Itsleaa
211475039
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.