The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Structures and Functions

C allows programmers to pass a single or entire structure information to or from a function. A structure information can be passed as a function arguments. The structure variable may be passed as a value or reference. The function will return the value by using the return statement.

C program - Passing Structure Members To Functions

Lets code and have some fun

structure-functions.c
#include <stdio.h>
int add(int, int) ;     //function declaration
int main()
{
//structures declartion
struct addition{
    int a, b;
    int c; 
}sum;
printf("Enter the value of a : ");
scanf("%d",&sum.a);
printf("\nEnter the value of b : ");
scanf("%d",&sum.b);
sum.c = add(sum. a, sum.b);     //passing structure members as arguments to function
printf("\nThe sum of two value are : ");
printf("%d ", sum.c);
return 0;
}
//Function definition
int add(int x, int y)
{
int sum1;
sum1 = x + y;
return(sum1);
}
  • Enter the value of a 10
  • Enter the value of b 20
  • The sum of two values 30

Note:

The structure addition have three variables( a, b, c) which are also known as structure members. The variables are reads through scanf function. The next statement illustrates that the structure members can be passed to the function add. The add function defined to perform addition operation between two values.

C program - Passing The Entire Structure To Functions

Lets code and have some fun

structure-functions-1.c
#include <stdio.h>
//structures declaration
typedef struct {
    int a, b;
    int c;
}sum;
void add(sum) ;     //function declaration with struct type sum
int main()
{
sum s1;
printf("Enter the value of a : ");
scanf("%d",&s1.a);
printf("\nEnter the value of b : ");
scanf("%d",&s1.b);
add(s1);     //passing entire structure as an argument to function
return 0;
}
//Function Definition
void add(sum s)
{
int sum1;
sum1 = s.a + s.b;
printf("\nThe sum of two values are :%d ", sum1);
}
  • Enter the value of a 10
  • Enter the value of b 20
  • The sum of two values 30

Note:

The program uses the keyword typedef to create the structure type sum. The variable "s1" is declared as structure type sum.

Passing Structures Using Pointers

The Call by value method is inefficient to pass the large structures to functions. C provides the efficient method, known as call by reference to pass large structures to functions. Call by reference method is achieved by passing pointers to functions. Pointer is a variable, used to hold or store the address of another variable.

C program - passing Structures By Using Pointers

Lets code and have some fun

structure-functions-2.c
#include <stdio.h>
//Structure declartion
struct employee {
    char name[40];
    int empid;
    int experience;
}emp;
void displaydetails(struct employee*);  //function declaration
int main()
{
struct employee *empptr;   //pointer declaration
empptr = &emp;   //initial
printf("\nEnter the name of the Employee : ");
scanf("%s", empptr->name);
printf("\nEnter the Employee Id : ");
scanf("%d",&empptr->empid);
printf("\nEnter Experience of the Employee : ");
scanf("%d",&empptr->experience);
displaydetails(empptr);
return 0;
}
//Function Definition
void displaydetails(struct employee *empptr)
{
printf("\n---------Details List--------- \n ");
printf("Employee Name : %s",empptr->name);
printf("\nEmployee ID : %d ",empptr->empid);
printf("\nEmployee Experience : %d ",empptr->experience);
}
  • Enter the name of the Employee : Jiju
  • Enter the Employee Id : 16
  • Enter Experience of the Employee : 3
  • ---------Details List---------
  • Employee Name : Jiju
  • Employee Id : 16
  • Employee Experience : 3

Note:

The structure employee has the variable "emp". The pointer variable is declared inside the main function and initialized as &emp. Now the pointer empptr hold the address of structure. The employee structure members can be accessed by using the "empptr->membername". The calling function displaydetails has a pointer "empptr" as an argument. Finally the function will display the details of employee.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report