Functions and Character Double Dimension Array

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 array_name[Row][Column];

Consider a character double dimension array

char a[3][4];

Let us answer few basic questions in this array

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

Function Call

Function Definition

Observations

fun(a[0][0])

void fun(char x) {}

  • Call by Value

fun(a[1][0])

void fun(char x) {}

  • Call by Value

fun(a[2][0])

void fun(char x) {}

  • Call by Value

fun(&a[0][0])

void fun(char *p) { }

  • Call by Reference

fun(&a[1][0])

void fun(char *p) { }

  • Call by Reference

fun(&a[2][0])

void fun(char *p) { }

  • Call by Reference

fun(**a)

void fun(char x) {}

  • Call by Value

fun(*(*(a + 1) + 0))

void fun(char x) {}

  • Call by Value

fun(*(*(a + 2) + 0))

void fun(char x) {}

  • Call by Value

fun(a[0])

void fun(char *p) { }

  • Call by Reference

fun(a[1])

void fun(char *p) { }

  • Call by Reference

fun(a[2])

void fun(char *p) { }

  • Call by Reference

fun(&a[0])

void fun(char (*p)[4]) { }

  • Call by Reference

fun(&a[1])

void fun(char (*p)[4]) { }

  • Call by Reference

fun(&a[2])

void fun(char (*p)[4]) { }

  • Call by Reference

fun(*a)

void fun(char *p) { }

  • Call by Reference

fun(*(a + 1))

void fun(char *p) { }

  • Call by Reference

fun(*(a + 2))

void fun(char *p) { }

  • Call by Reference

fun(a)

void fun(char (*p)[4]) { }

  • Call by Reference

fun(a + 1)

void fun(char (*p)[4]) { }

  • Call by Reference

fun(a + 2)

void fun(char (*p)[4]) { }

  • Call by Reference

fun(&a)

void fun(char (*p)[3][4]) { }

  • 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 an array

char a[3][4] = { "lap", "top", "123" };

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

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

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

Note : [ ] and * are dereference operators

Condition 3 : Expression DOES NOT have & operator

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

  • Step 1 : Consider an array

char a[3][4] = { "lap", "top", "123" };

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

  • Step 2 : Consider an expression **a

Condition 2 : Expression has TWO dereference operators * and *

Note : [ ] and * are dereference operators

Condition 3 : Expression DOES NOT have & operator

Hence **a 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 : &a[0][0]

If Declaration has two dereference operators, and

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

  • then it is call by reference

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

If Declaration has two dereference operators, and

  • Expression has zero dereference operators, and

  • then it is call by reference

  • Example : a, &a, a + 1, a + 2

  • Step 1 : Consider an array

char a[3][4] = { "lap", "top", "123" };

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

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

Condition 2 : Expression has TWO dereference operators * and *

Note : [ ] and * are dereference operators

Condition 3 : Expression has & operator

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

  • Step 1 : Consider an array

char a[3][4] = { "lap", "top", "123" };

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

  • Step 2 : Consider an expression a[1]

Condition 2 : Expression has ONE dereference operators

Note : [ ] and * are dereference operators

Condition 3 : Expression DOES NOT have & operator

Hence a[1] is Call By Reference

Let us look at examples of Call by Value

  • Step 1 : Consider a two dimensional array

char a[3][4] = { "lap", "top", "123" };
  • Step 2 : Pass a[0][0], a[1][0], a[1][1] to a function fun

fun(a[0][0]);

fun(a[1][0]);

fun(a[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 !

  • See full program below

#include <stdio.h>

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

int main(void)
{
        char a[3][4] = { "lap", "top", "123" };

        fun(a[0][0]);

        fun(a[1][0]);

        fun(a[1][1]);

        return 0;
}
  • Output is as below

char is l
char is t
char is o
  • Step 1 : Consider a two dimensional array

char a[3][4] = { "lap", "top", "123" };
  • Step 2 : Pass **a, *(*(a + 1) + 0), *(*(a + 1) + 1) to a function fun

fun( **a );

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

fun( *(*(a + 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 !

  • See full program below

#include <stdio.h>

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

int main(void)
{
        char a[3][4] = { "lap", "top", "123" };

        fun( **a );

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

        fun( *(*(a + 1) + 1) );

        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 two dimensional array

char a[3][4] = { "lap", "top", "123" };

There are 3 single dimension arrays in char a[3][4]

  • a[0]

  • a[1]

  • a[2]

a[0] is also equal to *a

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

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

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

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

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

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

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

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

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

fun( a[0] );

fun( a[1] );

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

fun( *a );

fun( *(a + 1) );

fun( *(a + 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");
}
  • See full program below

#include <stdio.h>

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

int main(void)
{
        char a[3][4] = { "lap", "top", "123" };

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

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

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

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

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

        fun( a[0] );

        fun( a[1] );

        fun( a[2] );

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

        fun( *a );

        fun( *(a + 1) );

        fun( *(a + 2) );

        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 two dimensional array

char a[3][4] = { "lap", "top", "123" };

There are 3 single dimension arrays in char a[3][4]

  • a[0]

  • a[1]

  • a[2]

Address of single dimension arrays is simply

  • &a[0]

  • &a[1]

  • &a[2]

  • &a[0] is also equal to a

  • &a[1] is also equal to a + 1

  • &a[2] is also equal to a + 2

  • Step 2.1 : Method 1 : Pass address of single dimension arrays to a function fun

fun( &a[0] );

fun( &a[1] );

fun( &a[2] );
  • Step 2.2 : Method 2 : Pass address of single dimension arrays to a function fun

fun( a );

fun( a + 1 );

fun( a + 2 );
  • Step 3.1 : Define the function fun

void fun(char (*ptr)[4] )
{
        printf("string is %s\n", *ptr);
}
  • Step 3.2 : Define the function fun to change the contents of single dimension array

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

        strcpy((char *)ptr, "Gap");
}
  • Step 3.3 : Define the function fun to change the contents of single dimension array character by character

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

        strcpy((char *)ptr, "Gap");

        (*ptr)[0] = 'p';
        (*ptr)[1] = 'g';
        (*ptr)[2] = 'x';
        (*ptr)[3] = '\0';
}
  • See full program below

#include <stdio.h>

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

        strcpy((char *)ptr, "Gap");

        (*ptr)[0] = 'p';
        (*ptr)[1] = 'g';
        (*ptr)[2] = 'x';
        (*ptr)[3] = '\0';
}

int main(void)
{
        char a[3][4] = { "lap", "top", "123" };

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

        fun( &a[0] );

        fun( &a[1] );

        fun( &a[2] );

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

        fun( a );

        fun( a + 1 );

        fun( a + 2 );

        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 pgx
string is pgx
string is pgx
  • Step 1 : Consider a two dimensional array

char a[3][4] = { "lap", "top", "123" };
  • Step 2 : Pass Address of Double Dimension array to a function

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

void fun(char (*ptr)[3][4] )
{

}
  • 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 < 3; 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 < 3; j++) {
                (*ptr)[i][j] = 'c';
        }
}
  • See full program below

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

void fun(char (*ptr)[3][4] )
{
        // 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 < 3; 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 < 3; j++) {
                        (*ptr)[i][j] = 'c';
                }
        }

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

int main(void)
{
        char a[3][4] = { "lap", "top", "123" };

        fun(&a);

        return 0;
}
  • Output is as below

lap
top
123
l
a
p
t
o
p
1
2
3
ccc
ccc
ccc