In C, structures can be passed as arguments to functions just like any other data type. This can be done by passing the structure variable, or a pointer to the structure, as an argument to the function.
Passing structure variables as arguments: When a structure variable is passed as an argument to a function, a copy of the structure is passed to the function. This means that any changes made to the structure inside the function will not affect the original structure.
This program demonstrates how to pass a structure as an argument to a function in C.
It's important to note that in this program, the structure member name is defined as a pointer to a character, which means it's pointing to a memory location that holds the name, it's not storing the name itself. So, it's important to make sure that the memory location pointed by the pointer is valid and has been allocated before passing the structure to the display function.
//Structure as function arguments in C Programming #include<stdio.h> struct student { char *name; int age; float per; }; void display(struct student o) { printf("\nName : %s",o.name); printf("\nAge : %d",o.age); printf("\nPercent : %f",o.per); } int main() { struct student o={"RAM",25,75.5}; display(o); return 0; }To download raw file Click Here
Name : RAM Age : 25 Percent : 75.500000
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions