Monday, 15 January 2018

Structures and Functions - Passing structures to functions in C

Passing structures to functions in C: When you are referring a structure variable is nothing but are referring whole structure i.e the total structure members. For structures to be fully useful, we must have a mechanism to pass them to functions and return them. A function may access the members of a structure in three ways. The are 

1. Passing individual members
2. Passing the entire structure 
3. Passing the address of the structure 
 
All these three ways are discussed here. Actually to pass any value / a derived type to a function we will use call by value and call by reference techniques / methods. The same process is also applied here to pass structures to functions.

1. Passing individual members

 To pass any individual member of the structure to a function, we must use the dot operator followed by structure member name as actual parameter in the function call and in function deceleration and definition we declare a variable to receive it.
 
Example: Write a c program to pass structure individual member to function. 
 
struct student
{
int no;
char name[30];
};
 
void display(int x);
void main()
{
struct student s={101,"Hari"};
display(s.no);   // passing individual member as actual parameter
getch();
}
void display(int x)
{
printf(" the student number is =%d",x);
}

2. Passing the entire structure 

The second method involves passing of a copy of the structure to the called function. Since the function is working on a copy of the structure, any changes to structure members within the function are not reflected in the original structure. It is therefore necessary for the function to return the entire structure back to the calling function. All compilers may not support this method of passing the entire structure as a parameter.

 To pass entire structure to a function we are actually passing the structure variable as actual parameter to the calling function, to receive values from calling function, the called must have formal parameter of matched type, hence we declare the formal parameters as structure variables. 

Example: Write a c program to pass entire structure to function using call by value method.
 
struct student
{
int no;
char name[30];
};
 
void display(struct student s);
void main()
{
struct student s={101,"Hari"};
display(s);
getch();
}
void display(struct student s)
{
printf(" the student number is =%d",s.x);
printf(" the student name is =%s",s.name);
}

3. Passing the address of the structure 

 As mentioned at the starting, when your are referring a structure variable is nothing but your are referring entire structure. So, to passing the entire structure to function using call by address method, we are actually passing the address of structure variable as actual parameter in the calling function, the called must have formal parameter of matched type, hence we declare the formal parameters as pointer variables.
 
Example: Write a c program to pass entire structure to function using call by value method.
 
struct student
{
int no;
char name[30];
};
 
void display(struct student *s);
void main()
{
struct student s={101,"Hari"};
display(&s);
getch();
}
void display(struct student *s)
{
printf(" the student number is =%d",s->x);
printf(" the student name is =%s",s->name);
}
 
Note: When structure variable is pointer we use the member selection operator -> to access the structure members. In the next post we will see how pointers are related to structure.

0 comments:

Post a Comment

Advertisement

Find Us On Facebook

C Programming

More

C++ Tutorial

More

Java Tutorial

More

Computer Basics

More

MS Office

More

Database Management

More
Top