Functions and Structure Single Pointer

In this section, you are going to learn

What are the calling conventions of structure single pointer ?

Call by Value

Call by Reference

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

struct ABC *sp;

Consider a Structure Single Pointer

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

struct ABC *sp;

Let us answer few basic questions about structure single pointer

If fun(x) is the function call, then fun(typeof(x)) is the prototype / definition

Function Call

Function Definition

Observations

fun(sp[0])

void fun(struct ABC x) { }

  • Call by Value

fun(sp[1])

void fun(struct ABC x) { }

  • Call by Value

fun(sp[4])

void fun(struct ABC x) { }

  • Call by Value

fun(&sp[0])

void fun(struct ABC *p) { }

  • Call by Reference

fun(&sp[1])

void fun(struct ABC *p) { }

  • Call by Reference

fun(&sp[4])

void fun(struct ABC *p) { }

  • Call by Reference

fun(*sp)

void fun(struct ABC x) { }

  • Call by Value

fun(*(sp + 1))

void fun(struct ABC x) { }

  • Call by Value

fun(*(sp + 4))

void fun(struct ABC x) { }

  • Call by Value

fun(sp)

void fun(struct ABC *p) { }

  • Call by Reference

fun(sp + 1)

void fun(struct ABC *p) { }

  • Call by Reference

fun(sp + 4)

void fun(struct ABC *p) { }

  • Call by Reference

fun(&sp)

void fun(struct ABC **p) { }

  • Call by Reference

If Declaration has ONE dereference operator, and

  • Expression has ONE dereference operator [], and

  • Expression does not have &

  • then it is call by value

If Declaration has ONE dereference operators, and

  • Expression has ONE dereference operator *, and

  • Expression does not have &

  • then it is call by value

If Declaration has ONE dereference operator, and

  • Expression has ONE dereference operators [] or *, and

  • Expression has ONE &

  • then it is call by reference

  • Example : &sp[0]

If Declaration has ONE dereference operator, and

  • Expression has ZERO dereference operator [ ] or *, and

  • Expression has ZERO & operator

  • then it is call by reference

  • Example : sp + 1, sp + 4

If Declaration has ONE dereference operator, and

  • Expression has ZERO dereference operator [ ] or *, and

  • Expression has ONE & operator

  • then it is call by reference

  • Example : &sp

Let us look at examples of Call by Value

Example for Call By Value with [ ]

  • Step 1 : Define a structure single pointer sp

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

struct ABC *sp;
  • Step 2 : Allocate heap memory of Bytes

sp = malloc(5 * sizeof(struct ABC));
  • Step 3 : Copy values to heap memory pointed to by sp

sp[0].a = 1; sp[0].b = 2; sp[0].c = 3;
sp[1].a = 4; sp[1].b = 5; sp[1].c = 6;
sp[2].a = 7; sp[2].b = 8; sp[2].c = 9;
sp[3].a = 10; sp[3].b = 11; sp[3].c = 12;
sp[4].a = 13; sp[4].b = 14; sp[4].c = 15;
  • Step 4 : Pass an individual structure sp[2] to a function. Call by Value

fun(sp[2]);

Individual heap elements can be accessed using [ ]

In this case sp[2] is third structure in the heap

sp[2] is fully dereferenced and there is no & symbol in fun(sp[2]). Hence this is Call By Value

  • Step 5 : Define function fun

void fun(struct ABC x)
{

}
  • Step 6 : Change value of x inside function fun

void fun(struct ABC x)
{
        x.a = 777;
        x.b = 888;
        x.c = 999;
}
  • Step 7 : Free heap memory after use

free(sp);
  • See the full program below

#include <stdio.h>
#include <stdlib.h>

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

void fun(struct ABC x)
{
        x.a = 777;
        x.b = 888;
        x.c = 999;
}

int main(void)
{
        struct ABC *sp;

        sp = malloc(5 * sizeof(struct ABC));

        sp[0].a = 1; sp[0].b = 2; sp[0].c = 3;
        sp[1].a = 4; sp[1].b = 5; sp[1].c = 6;
        sp[2].a = 7; sp[2].b = 8; sp[2].c = 9;
        sp[3].a = 10; sp[3].b = 11; sp[3].c = 12;
        sp[4].a = 13; sp[4].b = 14; sp[4].c = 15;

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

        fun(sp[2]);

        printf("----- After Call By Value -----\n");
        printf("sp[2].a = %d\n", sp[2].a);
        printf("sp[2].b = %d\n", sp[2].b);
        printf("sp[2].c = %d\n", sp[2].c);

        free(sp);

        return 0;
}
  • Output is as below

----- Before Call By Value -----
sp[2].a = 7
sp[2].b = 8
sp[2].c = 9

----- After Call By Value -----
sp[2].a = 7
sp[2].b = 8
sp[2].c = 9

Changing value of x inside function fun DOES NOT change sp[2]

Example for Call By Value with *

  • Step 1 : Define a structure single pointer sp

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

struct ABC *sp;
  • Step 2 : Allocate heap memory of Bytes

sp = malloc(5 * sizeof(struct ABC));
  • Step 3 : Copy string to heap memory pointed to by sp

sp[0].a = 1; sp[0].b = 2; sp[0].c = 3;
sp[1].a = 4; sp[1].b = 5; sp[1].c = 6;
sp[2].a = 7; sp[2].b = 8; sp[2].c = 9;
sp[3].a = 10; sp[3].b = 11; sp[3].c = 12;
sp[4].a = 13; sp[4].b = 14; sp[4].c = 15;
  • Step 4 : Pass an individual structure *(sp + 2) to a function. Call by Value

fun( *(sp + 2) );

Individual array elements can be accessed using *

In this case *(sp + 2) is third structure in the array

*(sp + 2) is fully dereferenced and there is no & symbol in fun( *(sp + 2) ). Hence this is Call By Value

  • Step 5 : Define function fun

void fun(struct ABC x)
{

}
  • Step 6 : Change value of x inside function fun

void fun(struct ABC x)
{
        x.a = 777;
        x.b = 888;
        x.c = 999;
}
  • Step 7 : Free heap memory after use

free(sp);
  • See the full program below

#include <stdio.h>
#include <stdlib.h>

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

void fun(struct ABC x)
{
        x.a = 777;
        x.b = 888;
        x.c = 999;
}

int main(void)
{
        struct ABC *sp;

        sp = malloc(5 * sizeof(struct ABC));

        sp[0].a = 1; sp[0].b = 2; sp[0].c = 3;
        sp[1].a = 4; sp[1].b = 5; sp[1].c = 6;
        sp[2].a = 7; sp[2].b = 8; sp[2].c = 9;
        sp[3].a = 10; sp[3].b = 11; sp[3].c = 12;
        sp[4].a = 13; sp[4].b = 14; sp[4].c = 15;

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

        fun( *(sp + 2) );

        printf("----- After Call By Value -----\n");
        printf(" (*(sp + 2)).a = %d\n", (*(sp + 2)).a );
        printf(" (*(sp + 2)).b = %d\n", (*(sp + 2)).b );
        printf(" (*(sp + 2)).c = %d\n", (*(sp + 2)).c );

        free(sp);

        return 0;
}
  • Output is as below

----- Before Call By Value -----
 (*(sp + 2)).a = 7
 (*(sp + 2)).b = 8
 (*(sp + 2)).c = 9

----- After Call By Value -----
 (*(sp + 2)).a = 7
 (*(sp + 2)).b = 8
 (*(sp + 2)).c = 9

Changing value of x inside function fun DOES NOT change *(sp + 2)

Remember sp[2] and *(sp + 2) are one and the same

Let us look at examples of Call by Reference

Example for Call By Reference with &sp[ ]

  • Step 1 : Define a structure single pointer sp

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

struct ABC *sp;
  • Step 2 : Allocate heap memory of Bytes

sp = malloc(5 * sizeof(struct ABC));
  • Step 3 : Copy values to heap memory pointed to by sp

sp[0].a = 1; sp[0].b = 2; sp[0].c = 3;
sp[1].a = 4; sp[1].b = 5; sp[1].c = 6;
sp[2].a = 7; sp[2].b = 8; sp[2].c = 9;
sp[3].a = 10; sp[3].b = 11; sp[3].c = 12;
sp[4].a = 13; sp[4].b = 14; sp[4].c = 15;
  • Step 4 : Pass address of an individual structure &sp[2] to a function. Call by Reference

fun( &sp[2] );

Address of individual heap elements can be accessed using &

In this case &sp[2] is the address of third structure in the heap

Since we are passing address of third structure to function fun, it is called call by reference with respect to third structure

  • Step 5 : Define function fun

void fun(struct ABC *x)
{

}
  • Step 6 : Change value of *x inside function fun

void fun(struct ABC *x)
{
        x->a = 777;
        x->b = 888;
        x->c = 999;
}
  • Step 7 : Free heap memory after use

free(sp);
  • See the full program below

#include <stdio.h>
#include <stdlib.h>

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

void fun(struct ABC *x)
{
        x->a = 777;
        x->b = 888;
        x->c = 999;
}

int main(void)
{
        struct ABC *sp;

        sp = malloc(5 * sizeof(struct ABC));

        sp[0].a = 1; sp[0].b = 2; sp[0].c = 3;
        sp[1].a = 4; sp[1].b = 5; sp[1].c = 6;
        sp[2].a = 7; sp[2].b = 8; sp[2].c = 9;
        sp[3].a = 10; sp[3].b = 11; sp[3].c = 12;
        sp[4].a = 13; sp[4].b = 14; sp[4].c = 15;

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

        fun( &sp[2] );

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

        free(sp);

        return 0;
}
  • Output is as below

----- Before Call By Value -----
sp[2].a = 7
sp[2].b = 8
sp[2].c = 9

----- After Call By Value -----
sp[2].a = 777
sp[2].b = 888
sp[2].c = 999

Changing value of *x inside function fun CHANGES sp[2] in sp

Example for Call By Reference with (sp + x)

  • Step 1 : Define a structure single pointer sp

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

struct ABC *sp;
  • Step 2 : Allocate heap memory of Bytes

sp = malloc(5 * sizeof(struct ABC));
  • Step 3 : Copy Values to heap memory pointed to by sp

sp[0].a = 1; sp[0].b = 2; sp[0].c = 3;
sp[1].a = 4; sp[1].b = 5; sp[1].c = 6;
sp[2].a = 7; sp[2].b = 8; sp[2].c = 9;
sp[3].a = 10; sp[3].b = 11; sp[3].c = 12;
sp[4].a = 13; sp[4].b = 14; sp[4].c = 15;
  • Step 4 : Pass address of individual structure sp + 2 to a function. Call by Reference

fun( sp + 2 );

In this case sp + 2 is the address of third structure in the heap

Since we are passing address of third structure to function fun, it is called call by reference with respect to third structure

  • Step 5 : Define function fun

void fun(struct ABC *x)
{

}
  • Step 6 : Change value of *x inside function fun

void fun(struct ABC *x)
{
        x->a = 777;
        x->b = 888;
        x->c = 999;
}
  • Step 7 : Free heap memory after use

free(sp);
  • See the full program below

#include <stdio.h>
#include <stdlib.h>

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

void fun(struct ABC *x)
{
        x->a = 777;
        x->b = 888;
        x->c = 999;
}

int main(void)
{
        struct ABC *sp;

        sp = malloc(5 * sizeof(struct ABC));

        sp[0].a = 1; sp[0].b = 2; sp[0].c = 3;
        sp[1].a = 4; sp[1].b = 5; sp[1].c = 6;
        sp[2].a = 7; sp[2].b = 8; sp[2].c = 9;
        sp[3].a = 10; sp[3].b = 11; sp[3].c = 12;
        sp[4].a = 13; sp[4].b = 14; sp[4].c = 15;

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

        fun( sp + 2 );

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

        free(sp);

        return 0;
}
  • Output is as below

----- Before Call By Reference -----
sp[2].a = 7
sp[2].b = 8
sp[2].c = 9

----- After Call By Reference -----
sp[2].a = 777
sp[2].b = 888
sp[2].c = 999

Changing value of *x inside function fun CHANGES sp[2]

  • Step 1 : Define a structure single pointer sp

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

struct ABC *sp;
  • Step 2 : Allocate heap memory of Bytes

sp = malloc(5 * sizeof(struct ABC));
  • Step 3 : Copy Values to heap memory pointed to by sp

sp[0].a = 1; sp[0].b = 2; sp[0].c = 3;
sp[1].a = 4; sp[1].b = 5; sp[1].c = 6;
sp[2].a = 7; sp[2].b = 8; sp[2].c = 9;
sp[3].a = 10; sp[3].b = 11; sp[3].c = 12;
sp[4].a = 13; sp[4].b = 14; sp[4].c = 15;
  • Step 4 : Pass full array array to a function

fun( sp );

Note that we are passing starting address of array

Hence function fun has read and write access to all Bytes of array

  • Step 5 : Define a function

void fun(struct ABC *x)
{
        int data = 99;

        for (int i = 0; i < 5; i++)
        {
                x[i].a = data++;
                x[i].b = data++;
                x[i].c = data++;
        }
}

function fun has access to all structures

  • Step 6 : Free heap memory after use

free(sp);
  • See full program below

#include <stdio.h>
#include <stdlib.h>

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

void fun(struct ABC *x)
{
        int data = 99;

        for (int i = 0; i < 5; i++)
        {
                x[i].a = data++;
                x[i].b = data++;
                x[i].c = data++;
        }
}

int main(void)
{
        struct ABC *sp;

        sp = malloc(5 * sizeof(struct ABC));

        sp[0].a = 1; sp[0].b = 2; sp[0].c = 3;
        sp[1].a = 4; sp[1].b = 5; sp[1].c = 6;
        sp[2].a = 7; sp[2].b = 8; sp[2].c = 9;
        sp[3].a = 10; sp[3].b = 11; sp[3].c = 12;
        sp[4].a = 13; sp[4].b = 14; sp[4].c = 15;

        printf("----- Before Call By Reference -----\n");
        for (int i = 0; i < 5; i++)
        {
                printf("sp[%d].a = %d\n", i, sp[i].a);
                printf("sp[%d].b = %d\n", i, sp[i].b);
                printf("sp[%d].c = %d\n", i, sp[i].c);
        }

        fun(sp);

        printf("----- After Call By Reference -----\n");
        for (int i = 0; i < 5; i++)
        {
                printf("sp[%d].a = %d\n", i, sp[i].a);
                printf("sp[%d].b = %d\n", i, sp[i].b);
                printf("sp[%d].c = %d\n", i, sp[i].c);
        }

        free(sp);

        return 0;
}
  • Output is as below

----- Before Call By Reference -----
sp[0].a = 1
sp[0].b = 2
sp[0].c = 3
sp[1].a = 4
sp[1].b = 5
sp[1].c = 6
sp[2].a = 7
sp[2].b = 8
sp[2].c = 9
sp[3].a = 10
sp[3].b = 11
sp[3].c = 12
sp[4].a = 13
sp[4].b = 14
sp[4].c = 15

----- After Call By Reference -----
sp[0].a = 99
sp[0].b = 100
sp[0].c = 101
sp[1].a = 102
sp[1].b = 103
sp[1].c = 104
sp[2].a = 105
sp[2].b = 106
sp[2].c = 107
sp[3].a = 108
sp[3].b = 109
sp[3].c = 110
sp[4].a = 111
sp[4].b = 112
sp[4].c = 113
  • Step 1 : Define a structure single pointer sp

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

struct ABC *sp;
  • Step 2 : Allocate heap memory of Bytes

sp = malloc(5 * sizeof(struct ABC));
  • Step 3 : Copy string to heap memory pointed to by sp

sp[0].a = 1; sp[0].b = 2; sp[0].c = 3;
sp[1].a = 4; sp[1].b = 5; sp[1].c = 6;
sp[2].a = 7; sp[2].b = 8; sp[2].c = 9;
sp[3].a = 10; sp[3].b = 11; sp[3].c = 12;
sp[4].a = 13; sp[4].b = 14; sp[4].c = 15;
  • Step 4 : Pass full array by reference with relative addressing

fun( sp + 2 );

Note that we are passing part of the array by reference

In this case, we are passing address of 3rd structure

Hence function fun has read and write access to structures at indexes 2, 3, 4 in forward direction

Hence function fun has read and write access to structures at indexes 0, 1 in backward direction

  • Step 5 : Define a function

void fun(struct ABC *x)
{
        x[-2].a = 100; // Same as sp[0]
        x[-1].a = 101; // Same as sp[1]
        x[0].a  = 102; // Same as sp[2]
        x[1].a  = 103; // Same as sp[3]
        x[2].a  = 104; // Same as sp[4]
}

Note the relative access mechanism used inside function fun

  • See full program below

#include <stdio.h>
#include <stdlib.h>

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

void fun(struct ABC *x)
{
        x[-2].a = 100; // Same as sp[0]
        x[-1].a = 101; // Same as sp[1]
        x[0].a  = 102; // Same as sp[2]
        x[1].a  = 103; // Same as sp[3]
        x[2].a  = 104; // Same as sp[4]
}

int main(void)
{
        struct ABC *sp;

        sp = malloc(5 * sizeof(struct ABC));

        sp[0].a = 1; sp[0].b = 2; sp[0].c = 3;
        sp[1].a = 4; sp[1].b = 5; sp[1].c = 6;
        sp[2].a = 7; sp[2].b = 8; sp[2].c = 9;
        sp[3].a = 10; sp[3].b = 11; sp[3].c = 12;
        sp[4].a = 13; sp[4].b = 14; sp[4].c = 15;

        printf("----- Before Call By Reference -----\n");
        for (int i = 0; i < 5; i++)
        {
                printf("sp[%d].a = %d\n", i, sp[i].a );
        }

        fun(sp + 2);

        printf("----- After Call By Reference -----\n");
        for (int i = 0; i < 5; i++)
        {
                printf("sp[%d].a = %d\n", i, sp[i].a );
        }

        free(sp);

        return 0;
}
  • Output is as below

----- Before Call By Reference -----
sp[0].a = 1
sp[1].a = 4
sp[2].a = 7
sp[3].a = 10
sp[4].a = 13

----- After Call By Reference -----
sp[0].a = 100
sp[1].a = 101
sp[2].a = 102
sp[3].a = 103
sp[4].a = 104
  • Step 1 : Define a structure single pointer sp

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

struct ABC *sp;
  • Step 2 : Allocate heap memory of Bytes

sp = malloc(5 * sizeof(struct ABC));
  • Step 3 : Copy string to heap memory pointed to by sp

sp[0].a = 1; sp[0].b = 2; sp[0].c = 3;
sp[1].a = 4; sp[1].b = 5; sp[1].c = 6;
sp[2].a = 7; sp[2].b = 8; sp[2].c = 9;
sp[3].a = 10; sp[3].b = 11; sp[3].c = 12;
sp[4].a = 13; sp[4].b = 14; sp[4].c = 15;
  • Step 4 : Pass the address of array sp to function fun

fun(&sp);
  • Step 5 : Define the function fun

void fun( struct ABC **dp)
{

}

Note that struct ABC **dp is a pointer to a structure single pointer

  • Step 6 : Access full string inside function fun

void fun( struct ABC **dp )
{
}
  • Step 7 : Access individual structures inside function fun

void fun( struct ABC **dp )
{
        //Change individual structures
        (*dp)[0].a = 99;
        (*dp)[1].a = 100;
        (*dp)[2].a = 101;
        (*dp)[3].a = 102;
        (*dp)[4].a = 103;
}
  • Step 8 : Free heap memory after use

free(sp);
  • See the full program below

#include <stdio.h>
#include <stdlib.h>

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

void fun( struct ABC **dp )
{
        //Change individual structures
        (*dp)[0].a = 99;
        (*dp)[1].a = 100;
        (*dp)[2].a = 101;
        (*dp)[3].a = 102;
        (*dp)[4].a = 103;
}

int main(void)
{
        struct ABC *sp;

        sp = malloc(5 * sizeof(struct ABC));

        sp[0].a = 1; sp[0].b = 2; sp[0].c = 3;
        sp[1].a = 4; sp[1].b = 5; sp[1].c = 6;
        sp[2].a = 7; sp[2].b = 8; sp[2].c = 9;
        sp[3].a = 10; sp[3].b = 11; sp[3].c = 12;
        sp[4].a = 13; sp[4].b = 14; sp[4].c = 15;

        printf("----- Before Call By Reference -----\n");
        for (int i = 0; i < 5; i++)
        {
                printf("sp[%d].a = %d\n", i, sp[i].a);
        }

        fun(&sp);

        printf("----- After Call By Reference -----\n");
        for (int i = 0; i < 5; i++)
        {
                printf("sp[%d].a = %d\n", i, sp[i].a);
        }

        free(sp);

        return 0;
}
  • Output is as below

----- Before Call By Reference -----
sp[0].a = 1
sp[1].a = 4
sp[2].a = 7
sp[3].a = 10
sp[4].a = 13

----- After Call By Reference -----
sp[0].a = 99
sp[1].a = 100
sp[2].a = 101
sp[3].a = 102
sp[4].a = 103