Functions and Structure

In this section, you are going to learn

What are the basic properties of a structure ?

What is Call by Value ?

What is Call by Reference ?

struct ABC {
        type1 member1;
        type2 member2;
        type3 member3;
};

struct ABC x;

Consider a Structure

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC x;

Let us answer few basic questions about a Structure

How many structures can be stored in x ?

How many bytes are there in this Structure ?

What is the sizeof the Structure ?

How many bits are there in this Structure ?

Consider a structure

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC x;

Then below are the properties of a structure

Expression

?

x

x is a structure

&x

&x is address of structure x

*x

NOT VALID

sizeof(x)

12 Bytes

sizeof(&x)

8 Bytes

typeof(x)

struct ABC

typeof(&x)

struct ABC *

Structure passed to a function

  • Step 1 : Define a structure

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC x;
  • Step 2 : Pass structure to a function

fun(x);
  • Step 3 : Define a function fun

void fun(struct ABC x)
{

}
  • Step 4 : Change structure inside function fun

void fun(struct ABC x)
{
        x.a = 10;
        x.b = 20;
        x.c = 30;
}
  • See full program below

#include <stdio.h>

struct ABC {
        int a;
        int b;
        int c;
};

void fun(struct ABC x)
{
        x.a = 10;
        x.b = 20;
        x.c = 30;
}

int main(void)
{
        struct ABC x = {.a = 1, .b = 2, .c = 3};

        printf("----- Before Call By Value -----\n");
        printf("x.a = %d, x.b = %d, x.c = %d\n", x.a, x.b, x.c);

        fun(x);

        printf("----- Before Call By Value -----\n");
        printf("x.a = %d, x.b = %d, x.c = %d\n", x.a, x.b, x.c);

        return 0;
}
  • Output is as below

----- Before Call By Value -----
x.a = 1, x.b = 2, x.c = 3

----- Before Call By Value -----
x.a = 1, x.b = 2, x.c = 3

Can you guess what is happening ?

After function fun is called,

  • There are two stack frames

  • Stack frame of main

  • Stack frame of fun

  • variable c is created on stack frame of main

  • variable c is created on stack frame of fun

  • Even though name of variable c is same in main and fun, they are two different variables in memory

  • Hence changing value of c inside function fun does not change the value of c inside main

Address of Structure passed to a function

  • Step 1 : Define a structure

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC x;
  • Step 2 : Pass address of structure to a function

fun(&x);
  • Step 3 : Define a function fun

void fun(struct ABC *p)
{

}
  • Step 4 : Change structure inside function fun

void fun(struct ABC *p)
{
        p->a = 10;
        p->b = 20;
        p->c = 30;
}
  • See full program below

#include <stdio.h>

struct ABC {
        int a;
        int b;
        int c;
};

void fun(struct ABC *p)
{
        p->a = 10;
        p->b = 20;
        p->c = 30;
}

int main(void)
{
        struct ABC x = {.a = 1, .b = 2, .c = 3};

        printf("----- Before Call By Reference -----\n");
        printf("x.a = %d, x.b = %d, x.c = %d\n", x.a, x.b, x.c);

        fun(&x);

        printf("----- After Call By Reference -----\n");
        printf("x.a = %d, x.b = %d, x.c = %d\n", x.a, x.b, x.c);

        return 0;
}
  • Output is as below

----- Before Call By Reference -----
x.a = 1, x.b = 2, x.c = 3

----- After Call By Reference -----
x.a = 10, x.b = 20, x.c = 30

Can you guess what is happening ?

Let us solve it with equations method !

  • Rule 1 : Base Rule

p = &x
  • RHS is actual parameter. In this case &x is actual parameter

  • LHS is formal parameter. In this case p is formal parameter

  • Rule 2 : Move & from RHS to LHS. This becomes * on LHS

*p = x
  • Rule 3 : Changing *p also changes x. Because we proved *p is equal to x

Function Call

Function Definition

Observations

fun(x)

void fun(struct ABC x) { }

Changing x inside fun DOES NOT change value of x in caller

fun(&x)

void fun(struct ABC *p) { }

Changing *p inside fun CHANGES value of x in caller