Learn how to pass structures to functions by value and by reference in C programming. Understand self-referential structures with examples and their use in linked lists and dynamic data structures.

Passing Structure to Function in C

Structures in C can be passed to functions in two ways:

  1. Pass by Value – A copy of the structure is passed. Changes inside the function do not affect the original structure.
  2. Pass by Reference (Using Pointer) – The address of the structure is passed. Changes inside the function affect the original structure.
  1. Pass by Value Example

#include <stdio.h>

#include <string.h>

struct Student {

    int roll_no;

    char name[50];

    float marks;

};

// Function that takes structure as parameter

void display(struct Student s) {

    printf(“Roll No: %d\n”, s.roll_no);

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

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

    // Modify structure member

    s.marks = 100;

}

int main() {

    struct Student s1 = {101, “Rahul”, 95.5};

    display(s1);  // Passing structure by value

    printf(“\nAfter function call, Marks: %.2f\n”, s1.marks);  // Original value unchanged

    return 0;

}

Output:

Roll No: 101

Name: Rahul

Marks: 95.50

After function call, Marks: 95.50

  1. Pass by Reference Using Pointer

#include <stdio.h>

#include <string.h>

struct Student {

    int roll_no;

    char name[50];

    float marks;

};

// Function that takes structure pointer

void updateMarks(struct Student *s) {

    s->marks = 100;  // Modifies original structure

}

int main() {

    struct Student s1 = {101, “Rahul”, 95.5};

    updateMarks(&s1);  // Passing structure by reference

    printf(“Updated Marks: %.2f\n”, s1.marks);  // Original value changed

    return 0;

}

Output:

Updated Marks: 100.00

Self-Referential Structure in C

A self-referential structure is a structure that contains a pointer to the same structure type. These are commonly used to create linked lists, trees, or other dynamic data structures.

Syntax:

struct node {

    int data;

    struct node *next;  // Pointer to the same structure

};

Example: Self-Referential Structure

#include <stdio.h>

#include <stdlib.h>

struct Node {

    int data;

    struct Node *next;

};

int main() {

    struct Node *head = NULL;

    struct Node *second = NULL;

    // Allocate memory for nodes

    head = (struct Node*)malloc(sizeof(struct Node));

    second = (struct Node*)malloc(sizeof(struct Node));

    // Assign values

    head->data = 10;

    head->next = second;

    second->data = 20;

    second->next = NULL;

    // Traversing the linked list

    struct Node *ptr = head;

    while(ptr != NULL) {

        printf(“%d -> “, ptr->data);

        ptr = ptr->next;

    }

    printf(“NULL\n”);

    return 0;

}

Output:

10 -> 20 -> NULL

Advantages of Self-Referential Structures

  1. Enables dynamic memory usage and efficient data management.
  2. Forms the basis for dynamic data structures like linked lists, trees, and graphs.
  3. Can grow or shrink during program execution.

SEO-Friendly Keywords

Passing structure to function in C, C structure pointer function, pass by value C structure, pass by reference C structure, self-referential structure in C, linked list structure C, dynamic data structures C, structure pointer example C, C programming structures.

SEO-friendly keywords:
, pointer to structure, , access structure members using pointer, pass structure pointer to function, C programming structures, arrow operator in C, structure and pointer in C.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Leave a Reply

Your email address will not be published. Required fields are marked *