MCS-201 · Unit 6

Structures and Unions

Section 3 of 5

Block 2 · Unit 6Structures and Unions12 min · Section 3 of 5

6.3Structures as Function Arguments

Covers IGNOU MCS-201 §6.5

Structures become really useful when functions can work on whole records: a function to print a student, another to compute a salary. This section shows how a structure travels into a function and back out.

Passing a structure by value

A structure is passed like any other variable: by value. The function receives a complete copy of the structure, so changes made inside the function do not affect the caller’s variable. (This differs from an array, which is passed as an address.)

For the function to know the type, the structure must be declared before the functions that use it, normally at the top of the file, outside all functions.

Returning a structure

A function can return a whole structure. Its return type is the structure type, and the result is assigned to a variable of the same type. This is one way for a function to give back several values at once.

Constant structure arguments

Since C99, a structure value can be written directly in a call with a compound literal: (struct tag){ values }.

C · compound.c
#include <stdio.h>

struct data {
    float amt;
    char fname[30];
};

void print_per(struct data x)
{
    printf("%s %.2f\n", x.fname, x.amt);
}

int main(void)
{
    print_per((struct data){ 1000.0f, "RAVI" });
    return 0;
}
Output
RAVI 1000.00

Value or pointer?

Pass by valuePass a pointer (Unit 7)
What the function getsA copy of the whole structureThe address of the original
Can it change the caller’s structure?NoYes
Cost for a large structureThe whole structure is copiedOnly an address is copied
Member access inside the functionx.memberp->member

Passing a pointer avoids copying and lets the function modify the original; members are then accessed with the arrow operator ->, covered with pointers in Unit 7.

Key takeaways

  • Structures are passed by value: the function works on a copy.
  • A function can return a structure, which is assigned to a variable of the same type.
  • Declare the structure type at file level, before the prototypes.
  • Compound literals (C99) create structure values directly in a call.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

When a structure is passed to a function by name, what does the function receive?

Show answer

Answer: A. A copy of the entire structure

Structures follow call by value, so every member is copied into the parameter.

Question 2 · multiple choice

What does this program print?

C · by_value_struct.c
#include <stdio.h>

struct acc { int balance; };

void deposit(struct acc a)
{
    a.balance = a.balance + 500;
}

int main(void)
{
    struct acc mine = { 1000 };
    deposit(mine);
    printf("%d\n", mine.balance);
    return 0;
}
Show answer

Answer: C. 1000

deposit changes only its copy a; mine in main is unchanged. Returning the structure or passing a pointer would be needed.

Question 3 · multiple choice

Where should a structure type be declared so that several functions can use it?

Show answer

Answer: D. At file level, before the functions and their prototypes

A declaration is visible only from where it appears to the end of its scope, so it must come first, outside all functions.

Question 4 · multiple choice

Which is a correct prototype for a function that takes a struct sal and returns a float?

Show answer

Answer: B. float wages(struct sal emp);

The parameter type is struct sal; struct alone is not a complete type name.

Question 5 · multiple choice

What does this program print?

C · return_struct.c
#include <stdio.h>

struct pt { int x; int y; };

struct pt make(int a)
{
    struct pt p = { a, a * 2 };
    return p;
}

int main(void)
{
    struct pt q = make(4);
    printf("%d %d\n", q.x, q.y);
    return 0;
}
Show answer

Answer: C. 4 8

make(4) builds a structure with x = 4 and y = 8 and returns a copy, which initialises q.

Question 6 · short answer

Rewrite deposit from question 2 so that the caller’s balance really increases, without using pointers, and show the call.

Show worked answer

Make the function return the updated structure: struct acc deposit(struct acc a) { a.balance = a.balance + 500; return a; }. The call becomes mine = deposit(mine);. The function still works on a copy, but the modified copy is returned and assigned back to mine, so mine.balance becomes 1500.

↑ ↓ move↵ openTab switch type