Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering C Structs: Passing, Returning, and Reference Handling

C Structures: Passing, Returning, and Reference Handling

In C programming, structures allow you to group related data into a single entity. This guide walks you through three essential operations with structs: passing them to functions, returning them from functions, and manipulating them by reference. Each section includes a concise explanation and a working example to solidify your understanding.


Passing Structs to Functions

Just like built‑in types, struct variables can be passed directly to a function. Before diving in, it’s helpful to review C structures, C functions, and user‑defined functions.

The following example demonstrates how to pass a struct student to a function that prints its contents.

#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("%[^
]%%*c", s1.name); // read until newline

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

   display(s1); // pass struct to function

   return 0;
}

void display(struct student s) {
   printf("\nDisplaying information\n");
   printf("Name: %s", s.name);
   printf("\nAge: %d", s.age);
}

Output

Enter name: Bond
Enter age: 13

Displaying information
Name: Bond
Age: 13

Returning a Struct from a Function

Functions can return entire structs, enabling a clean separation of data collection and processing. The example below collects student information inside a function and returns it to main.

#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("\nAge: %d", s.age);
    return 0;
}

struct student getInformation() {
    struct student s1;

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

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

    return s1;
}

The function’s return type matches the struct it returns, ensuring type safety and clarity.


Passing a Struct by Reference

When you need a function to modify a struct, pass its address (i.e., a pointer). This is analogous to passing built‑in types by reference. The following example adds two complex numbers and stores the result through a pointer.

#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;
}

Output

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

Because result is passed by reference, any changes inside addNumbers immediately affect the variable in main.


By mastering these techniques, you can write more modular, readable, and efficient C code that leverages the full power of structures.

C Language

  1. Hafnium Oxide (HfO₂): Structure, Properties, and Key Applications
  2. Valence Electrons, Crystal Structures, and Their Role in Conductivity
  3. Understanding C# Structs: Definition, Usage, and Key Differences
  4. C++ Friend Functions and Friend Classes: Mastering Access Control
  5. Mastering C Functions: User-Defined and Standard Library Basics
  6. Mastering C Structs and Pointers: A Practical Guide
  7. Structures vs. Unions in C: A Practical Guide
  8. Mastering C++ Overloading: Functions & Operators Explained
  9. Electrical Transformers Explained: Function, Design, and Industrial Applications
  10. Fiducial PCB Design: Purpose, Placement, and Best Practices