Functions and Character Double Pointer

In this section, you are going to learn

What are the calling conventions of character double dimension array ?

Call by Value

Call by Reference

char **dp;

Consider a character double dimension array

char **dp;

Let us answer few basic questions about character double pointer

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

Function Call

Function Definition

Observations

fun(dp[0][0])

void fun(char x) {}

  • Call by Value

fun(dp[1][0])

void fun(char x) {}

  • Call by Value

fun(dp[2][0])

void fun(char x) {}

  • Call by Value

fun(&dp[0][0])

void fun(char *p) { }

  • Call by Reference

fun(&dp[1][0])

void fun(char *p) { }

  • Call by Reference

fun(&dp[2][0])

void fun(char *p) { }

  • Call by Reference

fun(**dp)

void fun(char x) {}

  • Call by Value

fun(*(*(dp + 1) + 0))

void fun(char x) {}

  • Call by Value

fun(*(*(dp + 2) + 0))

void fun(char x) {}

  • Call by Value

fun(dp[0])

void fun(char *p) { }

  • Call by Reference

fun(dp[1])

void fun(char *p) { }

  • Call by Reference

fun(dp[2])

void fun(char *p) { }

  • Call by Reference

fun(&dp[0])

void fun(char **q) { }

  • Call by Reference

fun(*dp)

void fun(char *p) { }

  • Call by Reference

fun(*(dp + 1))

void fun(char *p) { }

  • Call by Reference

fun(*(dp + 2))

void fun(char *p) { }

  • Call by Reference

fun(dp)

void fun(char **q) { }

  • Call by Reference

fun(&dp)

void fun(char ***r) { }

  • Call by Reference

Let us understand the reason behind above prototypes !

If Declaration has two dereference operators, and

  • Expression has two dereference operators [] [], and

  • Expression does not have &

  • then it is call by value

If Declaration has two dereference operators, and

  • Expression has two dereference operators * *, and

  • Expression does not have &

  • then it is call by value

If Declaration has two dereference operators, and

  • Expression has two dereference operators * [ ], and

  • Expression does not have &

  • then it is call by value

Let us look at examples

  • Step 1 : Consider a double dimension array created using a character double pointer

char **dp;

dp = malloc(3 * sizeof(char *));

for (int i = 0; i < 3; i++)
{
        dp[i] = malloc(4 * sizeof(char));
}

strcpy(dp[0], "lap");
strcpy(dp[1], "top");
strcpy(dp[2], "123");

Condition 1 : Declaration has TWO dereference operators * and *

  • Step 2 : Consider an expression dp[1][1]

Condition 2 : Expression has TWO dereference operators [ ] and [ ]

Note : [ ] and * are dereference operators

Condition 3 : Expression DOES NOT have & operator

Hence dp[1][1] is Call By Value

  • Step 1 : Consider a double dimension array created using a character double pointer

char **dp;

dp = malloc(3 * sizeof(char *));

for (int i = 0; i < 3; i++)
{
        dp[i] = malloc(4 * sizeof(char));
}

strcpy(dp[0], "lap");
strcpy(dp[1], "top");
strcpy(dp[2], "123");

Condition 1 : Declaration has TWO dereference operators [ ] and [ ]

  • Step 2 : Consider an expression **dp

Condition 2 : Expression has TWO dereference operators * and *

Note : [ ] and * are dereference operators

Condition 3 : Expression DOES NOT have & operator

Hence **dp is Call By Value

If Declaration has two dereference operators, and

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

  • Expression has &

  • then it is call by reference

  • Example : &dp[0][0]

If Declaration has two dereference operators, and

  • Expression has one dereference operator [ ] or *

  • then it is call by reference

  • Example : &dp[0], dp[0], *dp

If Declaration has two dereference operators, and

  • Expression has zero dereference operators, and

  • then it is call by reference

  • Example : dp, &dp

  • Step 1 : Consider a double dimension array created using a character double pointer

char **dp;

dp = malloc(3 * sizeof(char *));

for (int i = 0; i < 3; i++)
{
        dp[i] = malloc(4 * sizeof(char));
}

strcpy(dp[0], "lap");
strcpy(dp[1], "top");
strcpy(dp[2], "123");

Condition 1 : Declaration has TWO dereference operators [ ] and [ ]

Condition 2 : Expression has TWO dereference operators * and *

Note : [ ] and * are dereference operators

Condition 3 : Expression has & operator

  • Step 2 : Consider an expression &dp[1][1]

Hence &dp[1][1] is Call By Reference

  • Step 1 : Consider a double dimension array created using a character double pointer

char **dp;

dp = malloc(3 * sizeof(char *));

for (int i = 0; i < 3; i++)
{
        dp[i] = malloc(4 * sizeof(char));
}

strcpy(dp[0], "lap");
strcpy(dp[1], "top");
strcpy(dp[2], "123");

Condition 1 : Declaration has TWO dereference operators [ ] and [ ]

  • Step 2 : Consider an expression dp[1]

Condition 2 : Expression has ONE dereference operators

Note : [ ] and * are dereference operators

Condition 3 : Expression DOES NOT have & operator

Hence dp[1] is Call By Reference

Let us look at examples of Call by Value

  • Step 1 : Consider a double dimension array created using a character double pointer

char **dp;

dp = malloc(3 * sizeof(char *));

for (int i = 0; i < 3; i++)
{
        dp[i] = malloc(4 * sizeof(char));
}

strcpy(dp[0], "lap");
strcpy(dp[1], "top");
strcpy(dp[2], "123");
  • Step 2 : Pass dp[0][0], dp[1][0], dp[1][1] to a function fun

fun(dp[0][0]);

fun(dp[1][0]);

fun(dp[1][1]);
  • Step 3 : Define function fun

void fun(char c)
{
        printf("char is %c\n", c);

        c = 'k';
}
  • Step 4 : Note that it is call by Value for below reason

Condition 1 : Declaration has TWO dereference operators [ ] and [ ]

Condition 2 : Expression has TWO dereference operators [ ] and [ ]

Condition 3 : Expression DOES NOT have & operator

Means changing value of character inside function DOES NOT affect value of character in Caller !

  • Step 5 : Free memory after use

for (int i = 0; i < 3; i++)
{
        free(dp[i]);
}

free(dp);
  • See full program below

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

void fun(char c)
{
        printf("char is %c\n", c);
}

int main(void)
{
        char **dp;

        dp = malloc(3 * sizeof(char *));

        for (int i = 0; i < 3; i++)
        {
                dp[i] = malloc(4 * sizeof(char));
        }

        strcpy(dp[0], "lap");
        strcpy(dp[1], "top");
        strcpy(dp[2], "123");

        fun(dp[0][0]);
        fun(dp[1][0]);
        fun(dp[1][1]);

        for (int i = 0; i < 3; i++)
        {
                free(dp[i]);
        }

        free(dp);

        return 0;
}
  • Output is as below

char is l
char is t
char is o
  • Step 1 : Consider a double dimension array created using a character double pointer

char **dp;

dp = malloc(3 * sizeof(char *));

for (int i = 0; i < 3; i++)
{
        dp[i] = malloc(4 * sizeof(char));
}

strcpy(dp[0], "lap");
strcpy(dp[1], "top");
strcpy(dp[2], "123");
  • Step 2 : Pass **dp, *(*(dp + 1) + 0), *(*(dp + 1) + 1) to a function fun

fun( **dp );

fun( *(*(dp + 1) + 0) );

fun( *(*(dp + 1) + 1) );
  • Step 3 : Define function fun

void fun(char c)
{
        printf("char is %c\n", c);

        c = 'k';
}
  • Step 4 : Note that it is call by Value for below reason

Condition 1 : Declaration has TWO dereference operators [ ] and [ ]

Condition 2 : Expression has TWO dereference operators * and *

Condition 3 : Expression DOES NOT have & operator

Means changing value of character inside function DOES NOT affect value of character in Caller !

  • Step 5 : Free memory after use

for (int i = 0; i < 3; i++)
{
        free(dp[i]);
}

free(dp);
  • See full program below

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

void fun(char c)
{
        printf("char is %c\n", c);
}

int main(void)
{
        char **dp;

        dp = malloc(3 * sizeof(char *));

        for (int i = 0; i < 3; i++)
        {
                dp[i] = malloc(4 * sizeof(char));
        }

        strcpy(dp[0], "lap");
        strcpy(dp[1], "top");
        strcpy(dp[2], "123");

        fun( **dp );

        fun( *(*(dp + 1) + 0) );

        fun( *(*(dp + 1) + 1) );

        for (int i = 0; i < 3; i++)
        {
                free(dp[i]);
        }

        free(dp);

        return 0;
}
  • Output is as below

char is l
char is t
char is o

Let us look at examples of Call by Reference

  • Step 1 : Consider a double dimension array created using a character double pointer

char **dp;

dp = malloc(3 * sizeof(char *));

for (int i = 0; i < 3; i++)
{
        dp[i] = malloc(4 * sizeof(char));
}

strcpy(dp[0], "lap");
strcpy(dp[1], "top");
strcpy(dp[2], "123");

There are 3 single dimension arrays

  • dp[0]

  • dp[1]

  • dp[2]

dp[0] is also equal to *dp

dp[1] is also equal to *(dp + 1)

dp[2] is also equal to *(dp + 2)

dp[0] is also equal to &dp[0][0]

dp[1] is also equal to &dp[1][0]

dp[2] is also equal to &dp[2][0]

  • Step 2.1 : Method 1 : Pass &dp[0][0], &dp[1][0], &dp[2][0] to a function fun

fun( &dp[0][0] );

fun( &dp[1][0] );

fun( &dp[2][0] );
  • Step 2.2 : Method 2 : Pass dp[0], dp[1], dp[2] to a function fun

fun( dp[0] );

fun( dp[1] );

fun( dp[2] );
  • Step 2.3 : Method 3 : Pass *dp, *(dp + 1), *(dp + 2) to a function fun

fun( *dp );

fun( *(dp + 1) );

fun( *(dp + 2) );
  • Step 3.1 : Define function fun

void fun(char *ptr)
{
        printf("string is %s\n", ptr);
}
  • Step 4 : Note that it is call by Reference. Means contents of single dimension array can be changed inside function fun

void fun(char *ptr)
{
        strcpy(ptr, "123");
}
  • Step 5 : Free memory after usage

for (int i = 0; i < 3; i++)
{
        free(dp[i]);
}

free(dp);
  • See full program below

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

void fun(char *ptr)
{
        printf("string is %s\n", ptr);
}

int main(void)
{
        char **dp;

        dp = malloc(3 * sizeof(char *));

        for (int i = 0; i < 3; i++)
        {
                dp[i] = malloc(4 * sizeof(char));
        }

        strcpy(dp[0], "lap");
        strcpy(dp[1], "top");
        strcpy(dp[2], "123");

        printf("Method 1 : Access Single dimension arrays\n");

        fun( &dp[0][0] );

        fun( &dp[1][0] );

        fun( &dp[2][0] );

        printf("Method 2 : Access Single dimension arrays\n");

        fun( dp[0] );

        fun( dp[1] );

        fun( dp[2] );

        printf("Method 3 : Access Single dimension arrays\n");

        fun( *dp );

        fun( *(dp + 1) );

        fun( *(dp + 2) );

        for (int i = 0; i < 3; i++)
        {
                free(dp[i]);
        }

        free(dp);

        return 0;
}
  • Output is as below

Method 1 : Access Single dimension arrays
string is lap
string is top
string is 123

Method 2 : Access Single dimension arrays
string is lap
string is top
string is 123

Method 3 : Access Single dimension arrays
string is lap
string is top
string is 123
  • Step 1 : Consider a double dimension array created using a character double pointer

char **dp;

dp = malloc(3 * sizeof(char *));

for (int i = 0; i < 3; i++)
{
        dp[i] = malloc(4 * sizeof(char));
}

strcpy(dp[0], "lap");
strcpy(dp[1], "top");
strcpy(dp[2], "123");
  • Step 2 : Pass Address of Double Dimension array to a function

fun(&dp);
  • Step 3.1 : Define function fun

void fun(char ***ptr )
{

}
  • Step 3.2 : Access and Print the strings inside function fun

for (int i = 0 ; i < 3; i++) {
        printf("%s\n", (*ptr)[i]);
}
  • Step 3.3 : Access and Print individual characters inside function fun

for (int i = 0 ; i < 3; i++) {
        for (int j = 0 ; j < 4; j++) {
                printf("%c\n", (*ptr)[i][j]);
        }
}
  • Step 3.4 : Access and change strings inside function fun

for (int i = 0 ; i < 3; i++) {
        strcpy( (*ptr)[i], "pgx" );
}
  • Step 3.5 : Access and change individual characters inside function fun

for (int i = 0 ; i < 3; i++) {
        for (int j = 0 ; j < 4; j++) {
                (*ptr)[i][j] = 'c';
        }
}
  • Step 4 : Free memory after usage

for (int i = 0; i < 3; i++)
{
        free(dp[i]);
}

free(dp);
  • See full program below

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

void fun(char ***ptr)
{
        // Print Individual strings
        for (int i = 0 ; i < 3; i++) {
                printf("%s\n", (*ptr)[i]);
        }

        // Print Individual characters
        for (int i = 0 ; i < 3; i++) {
                for (int j = 0 ; j < 4; j++) {
                        printf("%c\n", (*ptr)[i][j]);
                }
        }

        // Change Individual strings
        for (int i = 0 ; i < 3; i++) {
                strcpy( (*ptr)[i], "pgx" );
        }

        // Change Individual characters
        for (int i = 0 ; i < 3; i++) {
                for (int j = 0 ; j < 4; j++) {
                        (*ptr)[i][j] = 'c';
                }
        }

        // Print Individual strings
        for (int i = 0 ; i < 3; i++) {
                printf("%s\n", (*ptr)[i]);
        }
}

int main(void)
{
        char **dp;

        dp = malloc(3 * sizeof(char *));

        for (int i = 0; i < 3; i++)
        {
                dp[i] = malloc(4 * sizeof(char));
        }

        strcpy(dp[0], "lap");
        strcpy(dp[1], "top");
        strcpy(dp[2], "123");

        fun(&dp);

        for (int i = 0; i < 3; i++)
        {
                free(dp[i]);
        }

        free(dp);

        return 0;
}
  • Output is as below

lap
top
123
l
a
p

t
o
p

1
2
3

cccc
cccc
cccc