Functions and Character Triple Dimension Array
In this section, you are going to learn
What are the calling conventions of character triple dimension array ?
Call by Value
Call by Reference
Revisit Basics : Basics of Character Triple Dimension Array
Topics in this section,
char array_name[Block][Row][Column];
Consider a character triple dimension array
char a[3][4][5];
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][0]) |
void fun(char x) {} |
|
fun(a[1][0][0]) |
void fun(char x) {} |
|
fun(a[2][0][0]) |
void fun(char x) {} |
|
fun(&a[0][0][0]) |
void fun(char *p) {} |
|
fun(&a[1][0][0]) |
void fun(char *p) {} |
|
fun(&a[2][0][0]) |
void fun(char *p) {} |
|
fun(a[0][0]) |
void fun(char *p) {} |
|
fun(a[1][0]) |
void fun(char *p) {} |
|
fun(a[2][0]) |
void fun(char *p) {} |
|
fun(&a[0][0]) |
void fun(char (*p)[5]) {} |
|
fun(&a[1][0]) |
void fun(char (*p)[5]) {} |
|
fun(&a[2][0]) |
void fun(char (*p)[5]) {} |
|
fun(**a) |
void fun(char *p) {} |
|
fun(*(*(a + 1) + 0)) |
void fun(char *p) {} |
|
fun(*(*(a + 2) + 0)) |
void fun(char *p) {} |
|
fun(a[0]) |
void fun(char (*p)[5]) {} |
|
fun(a[1]) |
void fun(char (*p)[5]) {} |
|
fun(a[2]) |
void fun(char (*p)[5]) {} |
|
fun(&a[0]) |
void fun(char (*p)[4][5]) {} |
|
fun(&a[1]) |
void fun(char (*p)[4][5]) {} |
|
fun(&a[2]) |
void fun(char (*p)[4][5]) {} |
|
fun(*a) |
void fun(char (*p)[5]) {} |
|
fun(*(a + 1)) |
void fun(char (*p)[5]) {} |
|
fun(*(a + 2)) |
void fun(char (*p)[5]) {} |
|
fun(a) |
void fun(char (*p)[4][5]) {} |
|
fun(a + 1) |
void fun(char (*p)[4][5]) {} |
|
fun(a + 2) |
void fun(char (*p)[4][5]) {} |
|
fun(&a) |
void fun(char (*p)[3][4][5]) {} |
|
Let us understand the reason behind above prototypes !
If Declaration has THREE dereference operators, and
Expression has THREE dereference operators * * *, and
Expression does not have
&
then it is call by value
If Declaration has THREE dereference operators, and
Expression has THREE dereference operators * * [ ], and
Expression does not have
&
then it is call by value
If Declaration has THREE dereference operators, and
Expression has THREE dereference operators * [ ] [ ], and
Expression does not have
&
then it is call by value
If Declaration has THREE dereference operators, and
Expression has THREE 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][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
Condition 1 : Declaration has THREE dereference operators [ ], [ ] and [ ]
Step 2 : Consider an expression
a[1][1][1]
Condition 2 : Expression has THREE dereference operators [ ], [ ] and [ ]
Note : [ ]
and *
are dereference operators
Condition 3 : Expression DOES NOT have &
operator
Hence a[1][1][1]
is Call By Value
Step 1 : Consider an array
char a[3][4][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
Condition 1 : Declaration has THREE dereference operators [ ], [ ] and [ ]
Step 2 : Consider an expression
***a
Condition 2 : Expression has THREE dereference operators *, * and *
Note : [ ]
and *
are dereference operators
Condition 3 : Expression DOES NOT have &
operator
Hence ***a
is Call By Value
If Declaration has THREE dereference operators, and
Expression has THREE dereference operators * * * OR * * [] OR * [] [] OR [] [] [] and
Expression has &
then it is call by reference
Example : &a[0][0][0], &a[1][2][3]
If Declaration has THREE dereference operators, and
Expression has TWO dereference operator * * OR [] [] OR * []
then it is call by reference
Example : a[0][0]
If Declaration has THREE dereference operators, and
Expression has ONE dereference operators, * OR [ ]
then it is call by reference
Example : a[0]
If Declaration has THREE dereference operators, and
Expression has ZERO dereference operators
then it is call by reference
Example : a
Step 1 : Consider an array
char a[3][4][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
Condition 1 : Declaration has THREE dereference operators [ ] [ ] and [ ]
Step 2 : Consider an expression
&a[1][1][1]
Condition 2 : Expression has THREE dereference operators [ ] [ ] and [ ]
Note : [ ]
and *
are dereference operators
Condition 3 : Expression has &
operator
Hence &a[1][1][1]
is Call By Reference
Step 1 : Consider an array
char a[3][4][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
Condition 1 : Declaration has THREE dereference operators [ ] [ ] and [ ]
Step 2 : Consider an expression
a[1]
Condition 2 : Expression has ONE dereference operator
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 THREE dimensional array
char a[3][4][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
Step 2 : Pass a[0][0][0], a[1][0][0], a[2][0][0] to a function
fun
fun(a[0][0][0]);
fun(a[1][0][0]);
fun(a[2][0][0]);
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 THREE dereference operators [ ] [ ] and [ ]
Condition 2 : Expression has THREE 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);
c = 'k';
}
int main(void)
{
char a[3][4][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
fun(a[0][0][0]);
fun(a[1][0][0]);
fun(a[2][0][0]);
return 0;
}
Output is as below
char is l
char is g
char is s
Step 1 : Consider a three dimensional array
char a[3][4][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
Step 2 : Pass ***a, *(*(*(a + 1) + 0) + 0), *(*(*(a + 2) + 0) + 0) to a function
fun
fun( ***a );
fun( *(*(*(a + 1) + 0) + 0) );
fun( *(*(*(a + 2) + 0) + 0) );
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 THREE dereference operators [ ] [ ] and [ ]
Condition 2 : Expression has THREE 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][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
fun( ***a );
fun( *(*(*(a + 1) + 0) + 0) );
fun( *(*(*(a + 2) + 0) + 0) );
return 0;
}
Output is as below
char is l
char is g
char is s
Let us look at examples of Call by Reference
Step 1 : Consider a three dimensional array
char a[3][4][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
There are 12 single dimension arrays in char a[3][4]
a[0][0]
a[0][1]
a[0][2]
a[0][3]
a[1][0]
a[1][1]
a[1][2]
a[1][3]
a[2][0]
a[2][1]
a[2][2]
a[2][3]
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][0][0], &a[1][0][0], &a[2][0][0] to a function
fun
fun( &a[0][0][0] );
fun( &a[1][0][0] );
fun( &a[2][0][0] );
Step 2.3 : Method 3 : Pass **a, *(*(a + 1) + 0), *(*(a + 2) + 0) to a function
fun
fun( **a );
fun( *(*(a + 1) + 0) );
fun( *(*(a + 2) + 0) );
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][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
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][0][0] );
fun( &a[1][0][0] );
fun( &a[2][0][0] );
printf("Method 3 : Access Single dimension arrays\n");
fun( **a );
fun( *(*(a + 1) + 0) );
fun( *(*(a + 2) + 0) );
return 0;
}
Output is as below
Method 1 : Access Single dimension arrays
string is lap0
string is gap0
string is sap0
Method 2 : Access Single dimension arrays
string is lap0
string is gap0
string is sap0
Method 3 : Access Single dimension arrays
string is lap0
string is gap0
string is sap0
Step 1 : Consider a THREE dimensional array
char a[3][4][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
There are 12 single dimension arrays in char a[3][4][5]
a[0][0]
a[0][1]
a[0][2]
a[0][3]
a[1][0]
a[1][1]
a[1][2]
a[1][3]
a[2][0]
a[2][1]
a[2][2]
a[2][3]
Address of single dimension arrays is simply
&a[0][0]
&a[0][1]
&a[0][2]
&a[0][3]
&a[1][0]
&a[1][1]
&a[1][2]
&a[1][3]
&a[2][0]
&a[2][1]
&a[2][2]
&a[2][3]
Step 2.1 : Method 1 : Pass address of single dimension arrays to a function
fun
fun( &a[0][0] );
fun( &a[1][0] );
fun( &a[2][0] );
Step 2.2 : Method 2 : Pass address of single dimension arrays to a function
fun
fun( a[0] );
fun( a[1] );
fun( a[2] );
Step 2.3 : 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)[5] )
{
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)[5] )
{
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)[5] )
{
strcpy((char *)ptr, "Gap");
(*ptr)[0] = 'p';
(*ptr)[1] = 'g';
(*ptr)[2] = 'x';
(*ptr)[3] = 'y';
(*ptr)[4] = '\0';
printf("string is %s\n", *ptr);
}
See full program below
#include <stdio.h>
void fun(char (*ptr)[5] )
{
strcpy((char *)ptr, "Gap");
(*ptr)[0] = 'p';
(*ptr)[1] = 'g';
(*ptr)[2] = 'x';
(*ptr)[3] = 'y';
(*ptr)[4] = '\0';
printf("string is %s\n", *ptr);
}
int main(void)
{
char a[3][4][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
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 pgxy
string is pgxy
string is pgxy
Method 2 : Access Single dimension arrays
string is pgxy
string is pgxy
string is pgxy
Method 3 : Access Single dimension arrays
string is pgxy
string is pgxy
string is pgxy
Step 1 : Consider a three dimensional array
char a[3][4][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
Step 2 : Pass Double dimension array to function
fun
fun(a[1]);
Step 3.1 : Define function
fun
void fun(char (*ptr)[5])
{
}
Step 3.2 : Access and Print strings inside function
fun
void fun(char (*ptr)[5])
{
for(int i = 0; i < 4; i++)
{
printf("%s", ptr[i]);
printf("\n");
}
}
Step 3.3 : Access and Print characters inside function
fun
void fun(char (*ptr)[5])
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 5; j++)
{
printf("%c", ptr[i][j]);
}
printf("\n");
}
}
Step 3.4 : Access and Change strings inside function
fun
void fun(char (*ptr)[5])
{
for(int i = 0; i < 4; i++)
{
strncpy(ptr[i], "gggg", 5);
}
}
Step 3.5 : Access and Change characters inside function
fun
void fun(char (*ptr)[5])
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 5 - 1; j++)
{
ptr[i][j] = 'c';
}
}
}
Step 4 : See the full program below
#include <stdio.h>
#include <string.h>
void fun(char (*ptr)[5])
{
// Access Strings and Print
printf("fun() : Access Strings and Print\n");
for(int i = 0; i < 4; i++)
{
printf("%s", ptr[i]);
printf("\n");
}
// Access Characters and Print
printf("fun() : Access Characters and Print\n");
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 5; j++)
{
printf("%c", ptr[i][j]);
}
printf("\n");
}
//Access Strings and Change
for(int i = 0; i < 4; i++)
{
strncpy(ptr[i], "gggg", 5);
}
printf("fun() : Access Strings and Print\n");
for(int i = 0; i < 4; i++)
{
printf("%s", ptr[i]);
printf("\n");
}
// Access Characters and Change
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 5 - 1; j++)
{
ptr[i][j] = 'c';
}
}
printf("fun() : Access Strings and Print\n");
for(int i = 0; i < 4; i++)
{
printf("%s", ptr[i]);
printf("\n");
}
}
int main(void)
{
char a[3][4][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
fun(a[1]);
return 0;
}
Step 5 : Output is as below
fun() : Access Strings and Print
gap0
pop0
yyy0
4560
fun() : Access Characters and Print
gap0
pop0
yyy0
4560
fun() : Access Strings and Print
gggg
gggg
gggg
gggg
fun() : Access Strings and Print
cccc
cccc
cccc
cccc
Step 1 : Consider a three dimensional array
char a[3][4][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
Step 2 : Pass Address of Double dimension array to function
fun
fun(&a[1]);
Step 3.1 : Define function
fun
void fun(char (*ptr)[4][5])
{
}
Step 3.2 : Access and Print strings inside function
fun
void fun(char (*ptr)[4][5])
{
for(int i = 0; i < 4; i++)
{
printf("%s", (*ptr)[i]);
printf("\n");
}
}
Step 3.3 : Access and Print characters inside function
fun
void fun(char (*ptr)[4][5])
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 5; j++)
{
printf("%c", (*ptr)[i][j]);
}
printf("\n");
}
}
Step 3.4 : Access and Change strings inside function
fun
void fun(char (*ptr)[4][5])
{
for(int i = 0; i < 4; i++)
{
strncpy( (*ptr)[i], "gggg", 5);
}
}
Step 3.5 : Access and Change characters inside function
fun
void fun(char (*ptr)[4][5])
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 5 - 1; j++)
{
(*ptr)[i][j] = 'c';
}
}
}
Step 4 : See the full program below
#include <stdio.h>
#include <string.h>
void fun(char (*ptr)[4][5])
{
// Access Strings and Print
printf("fun() : Access Strings and Print\n");
for(int i = 0; i < 4; i++)
{
printf("%s", (*ptr)[i]);
printf("\n");
}
// Access Characters and Print
printf("fun() : Access Characters and Print\n");
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 5; j++)
{
printf("%c", (*ptr)[i][j]);
}
printf("\n");
}
//Access Strings and Change
for(int i = 0; i < 4; i++)
{
strncpy((*ptr)[i], "gggg", 5);
}
printf("fun() : Access Strings and Print\n");
for(int i = 0; i < 4; i++)
{
printf("%s", (*ptr)[i]);
printf("\n");
}
// Access Characters and Change
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 5 - 1; j++)
{
(*ptr)[i][j] = 'c';
}
}
printf("fun() : Access Strings and Print\n");
for(int i = 0; i < 4; i++)
{
printf("%s", (*ptr)[i]);
printf("\n");
}
}
int main(void)
{
char a[3][4][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
fun(&a[1]);
return 0;
}
Step 5 : Output is as below
fun() : Access Strings and Print
gap0
pop0
yyy0
4560
fun() : Access Characters and Print
gap0
pop0
yyy0
4560
fun() : Access Strings and Print
gggg
gggg
gggg
gggg
fun() : Access Strings and Print
cccc
cccc
cccc
cccc
Step 1 : Consider a three dimensional array
char a[3][4][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
Step 2 : Pass Address of Triple Dimension array to a function
fun(&a);
Step 3.1 : Define function
fun
void fun(char (*ptr)[3][4][5] )
{
}
Step 3.2 : Access and Print the strings inside function
fun
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
printf("(*ptr)[%d][%d] = %s\n", i, j, (*ptr)[i][j]);
}
}
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++)
{
for(int k = 0; k < 4; k++)
{
printf("(*ptr)[%d][%d][%d] = %c\n", i, j, k, (*ptr)[i][j][k]);
}
}
}
Step 3.4 : Access and change strings inside function
fun
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
strcpy( (*ptr)[i][j], "ccc");
}
}
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++)
{
for(int k = 0; k < 5; k++)
{
(*ptr)[i][j][k] = 'c';
}
}
}
See full program below
#include <stdio.h>
#include <string.h>
void fun(char (*ptr)[3][4][5] )
{
//Access and print individual strings
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
printf("(*ptr)[%d][%d] = %s\n", i, j, (*ptr)[i][j]);
}
}
//Access and print individual characters
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
for(int k = 0; k < 4; k++)
{
printf("(*ptr)[%d][%d][%d] = %c\n", i, j, k, (*ptr)[i][j][k]);
}
}
}
//Access and change individual strings
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
strcpy( (*ptr)[i][j], "ccc");
}
}
//Access and change individual characters
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
for(int k = 0; k < 5; k++)
{
(*ptr)[i][j][k] = 'c';
}
}
}
}
int main(void)
{
char a[3][4][5] = {
{
"lap0", "top0", "xxx0", "1230",
},
{
"gap0", "pop0", "yyy0", "4560" ,
},
{
"sap0", "gop0", "zzz0", "7890"
}
};
fun(&a);
return 0;
}
Other topics of character and functions
Current Module
Previous Module
Next Module
Other Modules