English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Others

C Language Reference Manual

C Structures and Functions

In this tutorial, you will learn how to pass a structure variable as a parameter to a function. You will learn how to return a struct from a function with examples.

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

Passing a Structure to a Function

We recommend that you learn these tutorials first before learning how to pass a structure to a function.

This is the method to pass a 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: ");
    //Reads the user input string until the newline character is entered
    // The newline is discarded
   scanf("%[^\n]%*c",*c", s1.name);
   printf("Enter age: ");
   scanf("%d", &s1.age);
   display(s1); //Passing a structure as a parameter
   return 0;
}
void display(struct student s) {
   printf("\nDisplay Information\n");
   printf("Name: %s", s.name);
   printf("\nAge: %d", s.age);
}

Output result

Enter Name: Bond
Enter Age: 13
Display Information
Name: Bond
Age: 13

In this case, a struct student variable s of type struct student is created1. Use display(s1pass the variable to the display() function declaration.

Returning a Structure from a Function

This is the method to return a 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("\nDisplay 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", &s*c", s1.name);
  printf("Enter age: ");
  scanf("%d", &s1.age);
  
  return s1;
}	

Here, use s = getInformation() to call the getInformation() function declaration. The function returns a struct student type structure. Display the returned structure in the main() function.

Note that the return type of getInformation() is also struct student.

Passing structures by reference

You can also pass structures by reference (just like you pass built-in type variables by reference). We recommend that you readReferenceGuide.

During the period of passing by reference, the memory address of the structure variable will be 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("Enter the first number:\n");
    printf("Enter the real part: ");
    scanf("%f", &c1.real);
    printf("Enter the imaginary part: ");
    scanf("%f", &c1.imag);
    printf("Enter the second number: 
");
    printf("Enter the real part: ");
    scanf("%f", &c2.real);
    printf("Enter the 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;
}

Output result

Enter the first number:
Enter the real part: 5.8
Enter the imaginary part: -3.4
Enter the second number:
Enter the real part: 9.9
Enter the imaginary part: -4.5
result.real = 15.7
result.imag = -7.9

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

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