How to pass individual members of structure as arguments to function in C language?

In C programming, you can pass individual members of a structure as arguments to functions. This approach treats each structure member as a separate parameter, allowing functions to work with specific data fields independently.

Syntax

functionName(structure_variable.member1, structure_variable.member2, ...);

Key Points

  • Each member is passed as an individual argument in the function call
  • Structure members are collected as ordinary variables in the function header
  • This method passes values by copy, not by reference

Example: Calculating Student Total Marks

Here's how to pass individual structure members to calculate total marks for students −

#include <stdio.h>

struct student {
    int s1, s2, s3;
};

void addition(int a, int b, int c, int student_num) {
    int sum = a + b + c;
    printf("Student %d scored total of %d<br>", student_num, sum);
}

int main() {
    struct student s[3];
    int i;
    
    /* Reading input for 3 students */
    for(i = 0; i < 3; i++) {
        printf("Enter marks for student %d in subject 1: ", i+1);
        scanf("%d", &s[i].s1);
        printf("Enter marks for student %d in subject 2: ", i+1);
        scanf("%d", &s[i].s2);
        printf("Enter marks for student %d in subject 3: ", i+1);
        scanf("%d", &s[i].s3);
    }
    
    /* Calling function with individual members */
    for(i = 0; i < 3; i++) {
        addition(s[i].s1, s[i].s2, s[i].s3, i+1);
    }
    
    return 0;
}

Example: Working with Date Structure

Another example showing how to pass date structure members individually −

#include <stdio.h>

struct date {
    int day;
    int month;
    int year;
};

void display_date(int d, int m, int y) {
    printf("day = %d<br>", d);
    printf("month = %d<br>", m);
    printf("year = %d<br>", y);
}

int main() {
    struct date today = {2, 1, 2010};
    
    /* Passing individual members */
    display_date(today.day, today.month, today.year);
    
    return 0;
}
day = 2
month = 1
year = 2010

Advantages and Disadvantages

Advantages Disadvantages
Simple function signatures More parameters for large structures
Works with specific members only Values passed by copy (no modification)
No need to pass entire structure Function calls become lengthy

Conclusion

Passing individual structure members as function arguments is useful when you need to work with specific fields only. However, for large structures, consider passing the entire structure or structure pointers for better efficiency and cleaner code.

Updated on: 2026-03-15T13:31:47+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements