C langugae : Passing struct to function

Recall that function arguments pass values to the function. Each value is a number—perhaps int, perhaps float, perhaps ASCII character code, or perhaps an address. A structure is a bit more complicated than a single value, so it is not surprising that ancient C implementations do not allow a structure to be used as an argument for a function. This limitation was removed in newer implementations, and ANSI C allows structures to be used as arguments. Therefore, modern implementations give you a choice between passing structures as arguments and passing pointers to structures as arguments—or if you are concerned with just part of a structure, you can pass structure members as arguments.
Passing Structure Members
As long as a structure member is a data type with a single value (that is, an int or one of its relatives, a char, a float, a double, or a pointer), it can be passed as a function argument to a function that accepts that particular type.
1.A structure can be passed to any function from main function or from any sub function.
2.Structure definition will be available within the function only.
3.It won’t be available to other functions unless it is passed to those functions by value or by address(reference).
4.Else, we have to declare structure variable as 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 to function in C:
It can be done in below 3 ways.
1. Passing structure to a function by value
2. Passing structure to a function by address(reference)
3. No need to pass a structure – Declare structure variable as global
1. Passing structure to a function by value: - The whole structure is passed to another function by value. It means the whole structure is passed to another function with all members and their values. So, this structure can be accessed from called function. This concept is very useful while writing very big programs in C.
#include < stdio.h >
#include < string.h >
struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student record);
int main()
{
struct student record;
record.id=1;
strcpy(record.name,"Anuj");
record.percentage=86.5;
func(record);
return 0;
}
void func(struct student record)
{
printf(" Id is : %d \n", record.id);
printf(" Name is : %s \n", record.name);
printf(" Percentage is : %f \n", record.percentage);
}
Output:
id is: 1
Name is: Anuj
Percentage is: 86.500000
2. Passing structure to a function by address(reference): - 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 *record);
int main()
{
struct student record;
record.id=1;
strcpy(record.name,"Anuj");
record.percentage=86.5;
func(&record);
return 0;
}
void func(struct student *record)
{
printf(" Id is : %d \n", record->id);
printf(" Name is : %s \n", record->name);
printf(" Percentage is : %f \n", record->percentage);
}
Output:
id is: 1
Name is: Anuj
Percentage is: 86.500000
3. 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.
#include < stdio.h >
#include < string.h >
struct student
{
int id;
char name[20];
float percentage;
};
struct student record; // Global declaration of structure
void structure_demo();
int main()
{
record.id=1;
strcpy(record.name,"Anuj");
record.percentage=86.5;
structure-demo(); return 0;
}
void structure_demo() {
printf(" Id is : %d \n", record.id);
printf(" Name is : %s \n", record.name);
printf(" Percentage is : %f \n", record.percentage);
}
Output:
id is: 1
Name is: Anuj
Percentage is: 86.500000

C langugae : Nested Structure

Nested structure in C is nothing but structure within structure. One structure can be declared inside other structure as we declare structure members inside a structure. The structure variables can be a normal structure variable or a pointer variable to access the data. You can learn below concepts in this section.
1. Structure within structure in C using normal variable
2. Structure within structure in C using pointer variable
1. Structure within structure in C using normal variable : - This program explains how to use structure within structure in C using normal variable. “student_college_detail’ structure is declared inside “student_detail” structure in this program. Both structure variables are normal structure variables. The members of “student_college_detail” structure are accessed by 2 dot(.) operator and members of “student_detail” structure are accessed by single dot(.) operator.
#include < stdio.h >
#include < string.h >
struct student_college_detail
{
int college_id;
char college_name[50]; };
struct student_detail
{
int id;
char name[20];
float percentage;
// structure within structure
struct student_college_detail clg_data;
}stu_data;
int main()
{
struct student_college_detail stu_data = {1, "Anuj", 72, 2889, "MDU University").;
printf(" Id is: %d \n", stu_data.id);
printf(" Name is: %s \n", stu_data.name);
printf(" Percentage is: %f \n\n", stu_data.percentage);
printf(" College Id is: %d \n", stu_data.clg_data.college_id);
printf(" College Name is: %s\n", stu_data.clg_data.college_name);
return 0;
}
Output : -
Id is: 1
Name is: Anuj
Percentage is: 72
College Id is: 2889
College Name is: MDU University
2. Structure within structure in C using pointer variable: - This program explains how to use structure within structure in C using pointer variable. “student_college_detail’ structure is declared inside “student_detail” structure in this program. one normal structure variable and one pointer structure variable is used in this program.
The combination of .(dot) and ->(arrow) operators are used to access the structure member which is declared inside the structure.
#include < stdio.h >
#include < string.h >
struct student_college_detail
{
int college_id;
char college_name[50]; };
struct student_detail
{
int id;
char name[20];
float percentage;
// structure within structure
struct student_college_detail clg_data;
}stu_data, *stu_data_ptr;
int main()
{
struct student_college_detail stu_data = {1, "Anuj", 72, 2889, "MDU University").;
stu_data_ptr=&stu_data;
printf(" Id is: %d \n", stu_data_ptr->id);
printf(" Name is: %s \n", stu_data_ptr->name);
printf(" Percentage is: %f \n\n", stu_data_ptr->percentage);
printf(" College Id is: %d \n", stu_data_ptr->clg_data.college_id);
printf(" College Name is: %s\n", stu_data_ptr->clg_data.college_name);
return 0;
}
Output : -
Id is: 1
Name is: Anuj
Percentage is: 72
College Id is: 2889
College Name is: MDU University


Free Web Hosting