# Structure and Function

Like other variables structure variable can be pass to a function as an argument, either we can pass entire structure or pointer to a structure according to our programming need. Also a function can return entire structure or pointer to structure.  Also as other C parameters a structure member can be pass by value or pass by reference to function.

Given below some example program to show how to pass an entire structure, pointer to structure, a member pf structure using pass by value and a member of structure using pass by reference.

Passing an entire structure as an argument to function.

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct num{

int value;

int value1;

};

void sum_of_member(struct num x)

{

int sum_of_values;

sum_of_values = x.value + x.value1;

printf(“Sum of structure members = %d\n”,sum_of_values);

x.value = 20;

x.value1 = 200;

}

int main()

{

struct num a;

int total;

a.value = 10;

a.value1 = 100;

sum_of_member(a);

total = a.value + a.value1;

printf(“Printing value of structure members after returning from function ‘sum_of_member’\n”);

printf(“a.value = %d\t a.value1 = %d\n”,a.value,a.value1);

}

Output: In output we can see any changes in called function does not effect in calling function.

\$ ./a.exe

Sum of structure members = 110

Printing value of structure members after returning from function ‘sum_of_member’

a.value = 10     a.value1 = 100

Passing pointer to structure as an argument to function.

#include<stdio.h>

#include<stdlib.h>

struct num{

int value;

int value1;

};

void sum_of_member(struct num *x)

{

int sum_of_values;

sum_of_values = x->value + x->value1;

printf(“Sum of structure members = %d\n”,sum_of_values);

x->value = 20;

x->value1 = 200;

}

int main()

{

struct num a;

int total;

a.value = 10;

a.value1 = 100;

sum_of_member(&a);

total = a.value + a.value1;

printf(“Printing value of structure members after returning from function ‘sum_of_member’\n”);

printf(“a.value = %d\t a.value1 = %d\n”,a.value,a.value1);

}

Output: In output we can see any changes in called function reflect in calling function.

\$ ./a.exe

Sum of structure members = 110

Printing value of structure members after returning from function ‘sum_of_member’

a.value = 20     a.value1 = 200

Passing a single member of structure using pass by value.

#include<stdio.h>

#include<stdlib.h>

struct num{

int value;

int value1;

};

void member(int x)

{

printf(“member value = %d\n”,x);

x = 20;

}

int main()

{

struct num a;

int total;

a.value = 10;

a.value1 = 100;

member(a.value);

total = a.value + a.value1;

printf(“Printing value of structure members after returning from function ‘member’\n”);

printf(“a.value = %d\t a.value1 = %d\n”,a.value,a.value1);

}

Output: In output we can see any changes in called function does not effect in calling function.

\$ ./a.exe

member value = 10

Printing value of structure members after returning from function ‘member’

a.value = 10     a.value1 = 100

Passing a single member of structure using pass by reference.

#include<stdio.h>

#include<stdlib.h>

struct num{

int value;

int value1;

};

void member(int *x)

{

printf(“member value = %d\n”,*x);

*x = 20;

}

int main()

{

struct num a;

int total;

a.value = 10;

a.value1 = 100;

member(&a.value);

total = a.value + a.value1;

printf(“Printing value of structure members after returning from function ‘member’\n”);

printf(“a.value = %d\t a.value1 = %d\n”,a.value,a.value1);

}

Output: In output we can see any changes in called function reflect in calling function.

\$ ./a.exe

member value = 10

Printing value of structure members after returning from function ‘member’

a.value = 20     a.value1 = 100

This site uses Akismet to reduce spam. Learn how your comment data is processed.