Structure and function in C programming

Here we will learn structure and function, how to pass a structure to function and return a structure from function. We can pass a variable as argument of a function. As structure is also a user defined variable, so we can also pass structure variable as an argument of function. In this article we will discuss about that. Before getting started you should see our following guide to get the better experience.

We will pass any structure variable to a function as like as other built in variables in C. So, let’s see how we can pass structure to function in C.

C program to pass structure to function

// C program to pass structure to function

#include <stdio.h>
struct student_info{
  char name[30];
  int age;
};

void display_info(struct student_info st){
  printf("\nDisplaying student information.........\n");
  printf("\nName : %s", st.name);
  printf("\nAge : %d\n", st.age);
}

int main(){
  struct student_info s1;

  printf("Enter name of student : ");

  scanf("%[^\n]%*c", s1.name);

  printf("Enter age of student : ");
  scanf("%d", &s1.age);

  display_info(s1);

  return 0;
}

Output of this structure and function program

structure and function in c, pass structure to function in c

How to return structure variable from a function?

// return structure variable from a function

#include <stdio.h>
struct student_information{
    char name[30];
    int age;
};

struct student_information get_information(){
  struct student_information s1;

  printf("Enter name of student : ");
  scanf ("%[^\n]%*c", s1.name);

  printf("Enter his age : ");
  scanf("%d", &s1.age);

  return s1;
}

int main(){
    struct student_information st;

    st = get_information();

    printf("\nDisplaying students information........\n");
    printf("Name : %s", st.name);
    printf("\nRoll : %d\n", st.age);

    return 0;
}

Program output

Passing structure variable by reference in C

We can also pass structure variable by reference to a function. In this time when we pass the structure by reference, the memory addresses of structure variable is passed to the function. Let’s see an example C program to pass structure variable by reference.

// how to Pass structure variable by reference in C

#include <stdio.h>
typedef struct comp_num{
  float real_part;
  float img_part;
} complex;

void add_number(complex c1, complex c2, complex *sum){
  sum -> real_part = c1.real_part + c2.real_part;
  sum -> img_part = c1.img_part + c2.img_part;
}

int main(){
  complex c1, c2, sum;

  printf("For first number : \n");
  printf("Enter real part here : ");
  scanf("%f", &c1.real_part);
  printf("Enter imaginary part here : ");
  scanf("%f", &c1.img_part);

  printf("For second number : \n");
  printf("Enter real part here : ");
  scanf("%f", &c2.real_part);
  printf("Enter imaginary part here : ");
  scanf("%f", &c2.img_part);

  add_number(c1, c2, &sum);
  printf("\nresult.real = %.1f\n", sum.real_part);
  printf("result.imag = %.1f\n", sum.img_part);

  return 0;
}

Compile and run the program. Then you will get the following output.

For first number :
Enter real part here : 4.8
Enter imaginary part here : -2.4

For second number :
Enter real part here : 5.3
Enter imaginary part here : -1.3

result.real = 10.1
result.imag = -3.7

Previous pageStructure and pointer in C

Next pageNested structure in C