Functions and Character Single Pointer

In this section, you are going to learn

What are the calling conventions of character single pointer ?

Call by Value

Call by Reference

char *sp;

Consider a Character Single Pointer

char *sp;

Let us answer few basic questions about character 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(char x) { }

  • Call by Value

fun(sp[1])

void fun(char x) { }

  • Call by Value

fun(sp[9])

void fun(char x) { }

  • Call by Value

fun(&sp[0])

void fun(char *p) { }

  • Call by Reference

fun(&sp[1])

void fun(char *p) { }

  • Call by Reference

fun(&sp[9])

void fun(char *p) { }

  • Call by Reference

fun(*sp)

void fun(char x) { }

  • Call by Value

fun(*(sp + 1))

void fun(char x) { }

  • Call by Value

fun(*(sp + 9))

void fun(char x) { }

  • Call by Value

fun(sp)

void fun(char *p) { }

  • Call by Reference

fun(sp + 1)

void fun(char *p) { }

  • Call by Reference

fun(sp + 9)

void fun(char *p) { }

  • Call by Reference

fun(&sp)

void fun(char **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 character single pointer sp

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

sp = malloc(10 * sizeof(char));
  • Step 3 : Copy string to heap memory pointed to by sp

strcpy(sp, "Laptop123");
  • Step 4 : Pass an individual character 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 character 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(char x)
{

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

void fun(char x)
{
        printf("x = %c\n", x);
        x = 'b';
}
  • Step 7 : Free heap memory after use

free(sp);
  • See the full program below

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

void fun(char x)
{
        printf("x = %c\n", x);
        x = 'b';
}

int main(void)
{
        char *sp;

        sp = malloc(10 * sizeof(char));

        strcpy(sp, "Laptop123");

        fun(sp[2]);

        printf("sp = %s\n", sp);

        free(sp);

        return 0;
}
  • Output is as below

x = p
sp = Laptop123

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

Example for Call By Value with *

  • Step 1 : Define a character single pointer sp

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

sp = malloc(10 * sizeof(char));
  • Step 3 : Copy string to heap memory pointed to by sp

strcpy(sp, "Laptop123");
  • Step 4 : Pass an individual character *(a + 2) to a function. Call by Value

fun( *(sp + 2) );

Individual array elements can be accessed using *

In this case *(sp + 2) is third character 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(char x)
{

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

void fun(char x)
{
        printf("x = %c\n", x);
        x = 'b';
}
  • Step 7 : Free heap memory after use

free(sp);
  • See the full program below

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

void fun(char x)
{
        printf("x = %c\n", x);
        x = 'b';
}

int main(void)
{
        char *sp;

        sp = malloc(10 * sizeof(char));

        strcpy(sp, "Laptop123");

        fun( *(sp + 2) );

        printf("sp = %s\n", sp);

        free(sp);

        return 0;
}
  • Output is as below

x = p
sp = Laptop123

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 character single pointer sp

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

sp = malloc(10 * sizeof(char));
  • Step 3 : Copy string to heap memory pointed to by sp

strcpy(sp, "Laptop123");
  • Step 4 : Pass address of an individual character &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 character in the heap

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

  • Step 5 : Define function fun

void fun(char *x)
{

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

void fun(char *x)
{
        printf("*x = %c\n", *x);
        *x = 'b';
}
  • Step 7 : Free heap memory after use

free(sp);
  • See the full program below

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

void fun(char *x)
{
        printf("*x = %c\n", *x);
        *x = 'b';
}

int main(void)
{
        char *sp;

        sp = malloc(10 * sizeof(char));

        strcpy(sp, "Laptop123");

        fun( &sp[2] );

        printf("sp = %s\n", sp);

        free(sp);

        return 0;
}
  • Output is as below

*x = p
sp = Labtop123

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

Example for Call By Reference with (sp + x)

  • Step 1 : Define a character single pointer sp

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

sp = malloc(10 * sizeof(char));
  • Step 3 : Copy string to heap memory pointed to by sp

strcpy(sp, "Laptop123");
  • Step 4 : Pass address of individual character sp + 2 to a function. Call by Reference

fun( sp + 2 );

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

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

  • Step 5 : Define function fun

void fun(char *x)
{

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

void fun(char *x)
{
        printf("*x = %c\n", *x);
        *x = 'b';
}
  • Step 7 : Free heap memory after use

free(sp);
  • See the full program below

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

void fun(char *x)
{
        printf("*x = %c\n", *x);
        *x = 'b';
}

int main(void)
{
        char *sp;

        sp = malloc(10 * sizeof(char));

        strcpy(sp, "Laptop123");

        fun( sp + 2 );

        printf("sp = %s\n", sp);

        free(sp);

        return 0;
}
  • Output is as below

*x = p
sp = Labtop123

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

  • Step 1 : Define a character single pointer sp

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

sp = malloc(10 * sizeof(char));
  • Step 3 : Copy string to heap memory pointed to by sp

strcpy(sp, "Laptop123");
  • 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(char *x)
{
        printf("x = %s\n", x);

        strcpy(x, "Laptop456");
}

function fun has access to all characters

  • Step 6 : Free heap memory after use

free(sp);
  • See full program below

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

void fun(char *x)
{
        printf("x = %s\n", x);

        strcpy(x, "Laptop456");
}

int main(void)
{
        char *sp;

        sp = malloc(10 * sizeof(char));

        strcpy(sp, "Laptop123");

        fun(sp);

        printf("sp = %s\n", sp);

        free(sp);

        return 0;
}
  • Output is as below

x = Laptop123
sp = Laptop456
  • Step 1 : Define a character single pointer sp

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

sp = malloc(10 * sizeof(char));
  • Step 3 : Copy string to heap memory pointed to by sp

strcpy(sp, "Laptop123");
  • Step 4 : Pass full array by reference with relative addressing

fun( sp + 5 );

Note that we are passing part of the array by reference

In this case, we are passing address of 6th character

Hence function fun has read and write access to Bytes 5, 6, 7, 8, 9 in forward direction

Hence function fun has read and write access to Bytes 0, 1, 2, 3, 4 in backward direction

  • Step 5 : Define a function

void fun(char *x)
{
        printf("x = %s\n", x);

        x[-5] = 'b'; // Same as a[0]
        x[-4] = 'a'; // Same as a[1]
        x[-3] = 'p'; // Same as a[2]
        x[-2] = 't'; // Same as a[3]
        x[-1] = 'a'; // Same as a[4]
        x[0]  = 'p'; // Same as a[5]
        x[1]  = '4'; // Same as a[6]
        x[2]  = '5'; // Same as a[7]
        x[3]  = '6'; // Same as a[8]
}

Note the relative access mechanism used inside function fun

  • See full program below

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

void fun(char *x)
{
        printf("x = %s\n", x);

        x[-5] = 'b'; // Same as a[0]
        x[-4] = 'a'; // Same as a[1]
        x[-3] = 'p'; // Same as a[2]
        x[-2] = 't'; // Same as a[3]
        x[-1] = 'a'; // Same as a[4]
        x[0]  = 'p'; // Same as a[5]
        x[1]  = '4'; // Same as a[6]
        x[2]  = '5'; // Same as a[7]
        x[3]  = '6'; // Same as a[8]
}

int main(void)
{
        char *sp;

        sp = malloc(10 * sizeof(char));

        strcpy(sp, "Laptop123");

        fun(sp + 5);

        printf("sp = %s\n", sp);

        free(sp);

        return 0;
}
  • Output is as below

x = p123
sp = baptap456
  • Step 1 : Define a character single pointer sp

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

sp = malloc(10 * sizeof(char));
  • Step 3 : Copy string to heap memory pointed to by sp

strcpy(sp, "Laptop123");
  • Step 4 : Pass the address of array sp to function fun

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

void fun( char **dp)
{

}

Note that char **dp is a pointer to a character single pointer

  • Step 6 : Access full string inside function fun

void fun( char **dp )
{
        printf("fun : String is %s\n", *dp);

        // Change full string
        strcpy(*dp, "Laptop456");
}
  • Step 7 : Access individual characters inside function fun

void fun( char **dp )
{
        //Change individual characters
        (*dp)[0] = 'g';
        (*dp)[1] = 'a';
        (*dp)[2] = 'p';
        (*dp)[3] = 't';
        (*dp)[4] = 'a';
        (*dp)[5] = 'p';
        (*dp)[6] = '4';
        (*dp)[7] = '5';
        (*dp)[8] = '6';
        (*dp)[9] = '\0';
}
  • Step 8 : Free heap memory after use

free(sp);
  • See the full program below

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

void fun( char **dp )
{
        //Change individual characters
        (*dp)[0] = 'g';
        (*dp)[1] = 'a';
        (*dp)[2] = 'p';
        (*dp)[3] = 't';
        (*dp)[4] = 'a';
        (*dp)[5] = 'p';
        (*dp)[6] = '4';
        (*dp)[7] = '5';
        (*dp)[8] = '6';
        (*dp)[9] = '\0';
}

int main(void)
{
        char *sp;

        sp = malloc(10 * sizeof(char));

        strcpy(sp, "Laptop123");

        fun(&sp);

        printf("main : String is %s\n", sp);

        free(sp);

        return 0;
}
  • Output is as below

main : String is gaptap456