C Programs Tutorials | IT Developer
IT Developer

C Programming - C Structures and Functions



Share with a Friend

C Programming - C Structures and Functions

C Structures and Functions

C allows structures to be used in functions, enabling the manipulation and processing of structured data. Structures can be passed to functions by value or by reference (using pointers), depending on the requirements.

Passing Structures to Functions

  1. By Value

When a structure is passed by value, a copy of the structure is created in the function's memory. Any changes made to the structure inside the function do not affect the original structure.

Example:

C

#include <stdio.h>

struct Point {

    int x;

    int y;

};

void display(struct Point p) {

    printf("Point: (%d, %d)\n", p.x, p.y);

}

int main() {

    struct Point p1 = {10, 20};

    display(p1);  // Passing by value

    return 0;

}

Output:

p>Point: (10, 20)
  1. By Reference

When a structure is passed by reference, a pointer to the structure is passed to the function. Any changes made to the structure inside the function directly affect the original structure.

Example:

C

#include <stdio.h>

struct Point {

    int x;

    int y;

};

void update(struct Point *p) {

    p->x += 10;

    p->y += 10;

}

int main() {

    struct Point p1 = {10, 20};

    printf("Before update: (%d, %d)\n", p1.x, p1.y);

    update(&p1);  // Passing by reference

    printf("After update: (%d, %d)\n", p1.x, p1.y);

    return 0;

}

Output:

Before update: (10, 20)

After update: (20, 30)

Returning Structures from Functions

Functions in C can also return structures. This is useful when you want a function to create and initialize a structure.

Example:

C

#include <stdio.h>

struct Point {

    int x;

    int y;

};

struct Point createPoint(int x, int y) {

    struct Point p;

    p.x = x;

    p.y = y;

    return p;

}

int main() {

    struct Point p1 = createPoint(10, 20);

    printf("Point: (%d, %d)\n", p1.x, p1.y);

    return 0;

}

Output:

Point: (10, 20)

Passing Arrays of Structures to Functions

You can also pass arrays of structures to functions for processing multiple records.

Example:

C

#include <stdio.h>

struct Point {

    int x;

    int y;

};

void displayPoints(struct Point points[], int size) {

    for (int i = 0; i < size; i++) {

        printf("Point %d: (%d, %d)\n", i + 1, points[i].x, points[i].y);

    }

}

int main() {

    struct Point points[3] = {{1, 2}, {3, 4}, {5, 6}};

    displayPoints(points, 3);

    return 0;

}

Output:

Point 1: (1, 2)

Point 2: (3, 4)

Point 3: (5, 6)

Use Case Example: Managing Student Records

Code:

C

#include <stdio.h>

#include <string.h>

struct Student {

    int id;

    char name[50];

    float marks;

};

void displayStudent(struct Student s) {

    printf("ID: %d\n", s.id);

    printf("Name: %s\n", s.name);

    printf("Marks: %.2f\n", s.marks);

}

int main() {

    struct Student s1;

    s1.id = 101;

    strcpy(s1.name, "Alice");

    s1.marks = 85.5;

    displayStudent(s1);

    return 0;

}

Output:

ID: 101

Name: Alice

Marks: 85.50

Advantages of Using Structures with Functions

  1. Modularity: Functions encapsulate the logic for processing structures.
  2. Reusability: Functions can be reused with different structures.
  3. Readability: Clear separation of logic and data enhances readability.
  4. Efficiency: Passing by reference avoids copying large structures.

Conclusion

Using structures with functions in C provides a robust way to manage and manipulate complex data. By leveraging functions to operate on structured data, programmers can write cleaner, modular, and reusable code.