C Structure and Function

In this article, you'll find relevant examples to pass structures as an argument to a function, and use them in your program. Also, you will learn to return structure from a function.

Similar like variables of built-in types, you can also pass structure variables to a function.


Passing structure to a function

Recommended readings before you proceed further:

Here's how you can pass structure to a function


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

// function prototype
void display(struct student s);

int main()
{
    struct student s1;

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

    printf("Enter age:");
    scanf("%d", &s1.age);
    
    display(s1);   // passing structure as an argument
    
    return 0;
}
void display(struct student s) 
{
  printf("\nDisplaying information\n");
  printf("Name: %s", s.name);
  printf("\nRoll: %d", s.age);
}

When you run the program, the output will be:


Enter name: Bond
Enter age: 13

Displaying information
Name: Bond
Roll: 13  

Here, a structure variable s1 of type struct student is created. This structure variable is passed to the display() function using display(s1); statement.


Returning structure from a function

Here's how you can return structure from a function:


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

// function prototype
struct student getInformation();

int main()
{
    struct student s;

    s = getInformation();

    printf("\nDisplaying information\n");
    printf("Name: %s", s.name);
    printf("\nRoll: %d", s.age);
    
    return 0;
}
struct student getInformation() 
{
  struct student s1;

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

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

Here, getInformation() function is called using s = getInformation(); statement. The function returns a structure of type struct student. The returned structure is displayed from the main() function.

Notice that, the return type of getInformation() is struct student.


Passing structure by reference

You can also pass a structure by reference (in a similar way like you pass variables of built-in type by reference). We suggest you to read pass by reference article before you proceed.

During pass by reference, the memory address of a structure is passed to the function.


#include <stdio.h>
typedef struct Complex
{
    float real;
    float imag;
} complex;

void addNumbers(complex c1, complex c2, complex *result); 

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

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

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

    addNumbers(c1, c2, &result); 
    printf("\nresult.real = %.1f\n", result.real);
    printf("result.imag = %.1f", result.imag);
    
    return 0;
}
void addNumbers(complex c1, complex c2, complex *result) 
{
     result->real = c1.real + c2.real;
     result->imag = c1.imag + c2.imag; 
}

When you run the program, the output will be:


For first number,
Enter real part:  1.1
Enter imaginary part:  -2.4
For second number, 
Enter real part:  3.4
Enter imaginary part:  -3.2

result.real = 4.5
result.imag = -5.6  

In the above program, three structure variables c1, c2 and the address of result is passed to the addNumbers() function. Here, result variable is passed by reference.

When result variable inside the addNumbers() is altered, result variable inside the main() function is also altered accordingly.