Functions and Integer Double Pointer
In this section, you are going to learn
What are the calling conventions of integer double dimension array ?
Call by Value
Call by Reference
Revisit Basics : Basics of Integer Double Pointers
Topics in this section,
int **dp;
Consider a integer double dimension array
int **dp;
Let us answer few basic questions about integer 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(int x) {} |
|
fun(dp[1][0]) |
void fun(int x) {} |
|
fun(dp[2][0]) |
void fun(int x) {} |
|
fun(&dp[0][0]) |
void fun(int *p) { } |
|
fun(&dp[1][0]) |
void fun(int *p) { } |
|
fun(&dp[2][0]) |
void fun(int *p) { } |
|
fun(**dp) |
void fun(int x) {} |
|
fun(*(*(dp + 1) + 0)) |
void fun(int x) {} |
|
fun(*(*(dp + 2) + 0)) |
void fun(int x) {} |
|
fun(dp[0]) |
void fun(int *p) { } |
|
fun(dp[1]) |
void fun(int *p) { } |
|
fun(dp[2]) |
void fun(int *p) { } |
|
fun(&dp[0]) |
void fun(int **q) { } |
|
fun(*dp) |
void fun(int *p) { } |
|
fun(*(dp + 1)) |
void fun(int *p) { } |
|
fun(*(dp + 2)) |
void fun(int *p) { } |
|
fun(dp) |
void fun(int **q) { } |
|
fun(&dp) |
void fun(int ***r) { } |
|
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 integer double pointer
int **dp;
dp = malloc(2 * sizeof(int *));
for (int i = 0; i < 2; i++)
{
dp[i] = malloc(3 * sizeof(int));
}
dp[0][0] = 11;
dp[0][1] = 22;
dp[0][2] = 33;
dp[1][0] = 44;
dp[1][1] = 55;
dp[1][2] = 66;
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 integer double pointer
int **dp;
dp = malloc(2 * sizeof(int *));
for (int i = 0; i < 2; i++)
{
dp[i] = malloc(3 * sizeof(int));
}
dp[0][0] = 11;
dp[0][1] = 22;
dp[0][2] = 33;
dp[1][0] = 44;
dp[1][1] = 55;
dp[1][2] = 66;
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 integer double pointer
int **dp;
dp = malloc(2 * sizeof(int *));
for (int i = 0; i < 2; i++)
{
dp[i] = malloc(3 * sizeof(int));
}
dp[0][0] = 11;
dp[0][1] = 22;
dp[0][2] = 33;
dp[1][0] = 44;
dp[1][1] = 55;
dp[1][2] = 66;
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 integer double pointer
int **dp;
dp = malloc(2 * sizeof(int *));
for (int i = 0; i < 2; i++)
{
dp[i] = malloc(3 * sizeof(int));
}
dp[0][0] = 11;
dp[0][1] = 22;
dp[0][2] = 33;
dp[1][0] = 44;
dp[1][1] = 55;
dp[1][2] = 66;
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 integer double pointer
int **dp;
dp = malloc(2 * sizeof(int *));
for (int i = 0; i < 2; i++)
{
dp[i] = malloc(3 * sizeof(int));
}
dp[0][0] = 11;
dp[0][1] = 22;
dp[0][2] = 33;
dp[1][0] = 44;
dp[1][1] = 55;
dp[1][2] = 66;
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(int c)
{
c = 99;
}
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 integer inside function DOES NOT affect value of integer in Caller !
Step 5 : Free memory after use
for (int i = 0; i < 2; i++)
{
free(dp[i]);
}
free(dp);
See full program below
#include <stdio.h>
#include <stdlib.h>
void fun(int c)
{
c = 99;
}
int main(void)
{
int **dp;
dp = malloc(2 * sizeof(int *));
for (int i = 0; i < 2; i++)
{
dp[i] = malloc(3 * sizeof(int));
}
dp[0][0] = 11;
dp[0][1] = 22;
dp[0][2] = 33;
dp[1][0] = 44;
dp[1][1] = 55;
dp[1][2] = 66;
printf("----- Before Call By Value -----\n");
printf("dp[0][0] = %d\n", dp[0][0]);
printf("dp[1][0] = %d\n", dp[1][0]);
printf("dp[1][1] = %d\n", dp[1][1]);
fun(dp[0][0]);
fun(dp[1][0]);
fun(dp[1][1]);
printf("----- After Call By Value -----\n");
printf("dp[0][0] = %d\n", dp[0][0]);
printf("dp[1][0] = %d\n", dp[1][0]);
printf("dp[1][1] = %d\n", dp[1][1]);
for (int i = 0; i < 2; i++)
{
free(dp[i]);
}
free(dp);
return 0;
}
Output is as below
----- Before Call By Value -----
dp[0][0] = 11
dp[1][0] = 44
dp[1][1] = 55
----- After Call By Value -----
dp[0][0] = 11
dp[1][0] = 44
dp[1][1] = 55
Step 1 : Consider a double dimension array created using a integer double pointer
int **dp;
dp = malloc(2 * sizeof(int *));
for (int i = 0; i < 2; i++)
{
dp[i] = malloc(3 * sizeof(int));
}
dp[0][0] = 11;
dp[0][1] = 22;
dp[0][2] = 33;
dp[1][0] = 44;
dp[1][1] = 55;
dp[1][2] = 66;
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(int c)
{
c = 99;
}
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 integer inside function DOES NOT affect value of integer in Caller !
Step 5 : Free memory after use
for (int i = 0; i < 2; i++)
{
free(dp[i]);
}
free(dp);
See full program below
#include <stdio.h>
#include <stdlib.h>
void fun(int c)
{
c = 99;
}
int main(void)
{
int **dp;
dp = malloc(2 * sizeof(int *));
for (int i = 0; i < 2; i++)
{
dp[i] = malloc(3 * sizeof(int));
}
dp[0][0] = 11;
dp[0][1] = 22;
dp[0][2] = 33;
dp[1][0] = 44;
dp[1][1] = 55;
dp[1][2] = 66;
printf("----- Before Call By Value -----\n");
printf(" **dp = %d\n", **dp);
printf(" *(*(dp + 1) + 0) = %d\n", *(*(dp + 1) + 0) );
printf(" *(*(dp + 1) + 1) = %d\n", *(*(dp + 1) + 1) );
fun( **dp );
fun( *(*(dp + 1) + 0) );
fun( *(*(dp + 1) + 1) );
printf("----- After Call By Value -----\n");
printf(" **dp = %d\n", **dp);
printf(" *(*(dp + 1) + 0) = %d\n", *(*(dp + 1) + 0) );
printf(" *(*(dp + 1) + 1) = %d\n", *(*(dp + 1) + 1) );
for (int i = 0; i < 2; i++)
{
free(dp[i]);
}
free(dp);
return 0;
}
Output is as below
----- Before Call By Value -----
**dp = 11
*(*(dp + 1) + 0) = 44
*(*(dp + 1) + 1) = 55
----- After Call By Value -----
**dp = 11
*(*(dp + 1) + 0) = 44
*(*(dp + 1) + 1) = 55
Let us look at examples of Call by Reference
Step 1 : Consider a double dimension array created using a integer double pointer
int **dp;
dp = malloc(2 * sizeof(int *));
for (int i = 0; i < 2; i++)
{
dp[i] = malloc(3 * sizeof(int));
}
dp[0][0] = 11;
dp[0][1] = 22;
dp[0][2] = 33;
dp[1][0] = 44;
dp[1][1] = 55;
dp[1][2] = 66;
There are 2 single dimension arrays
dp[0]
dp[1]
dp[0] is also equal to *dp
dp[1] is also equal to *(dp + 1)
dp[0] is also equal to &dp[0][0]
dp[1] is also equal to &dp[1][0]
Step 2.1 : Method 1 : Pass &dp[0][0], &dp[1][0] to a function
fun
fun( &dp[0][0] );
fun( &dp[1][0] );
Step 2.2 : Method 2 : Pass dp[0], dp[1] to a function
fun
fun( dp[0] );
fun( dp[1] );
Step 2.3 : Method 3 : Pass *dp, *(dp + 1) to a function
fun
fun( *dp );
fun( *(dp + 1) );
Step 3.1 : Define function
fun
void fun(int *ptr)
{
}
Step 4 : Note that it is call by Reference. Means contents of single dimension array can be changed inside function
fun
void fun(int *ptr)
{
ptr[0] = 77;
ptr[1] = 88;
ptr[2] = 99;
}
Step 5 : Free memory after usage
for (int i = 0; i < 2; i++)
{
free(dp[i]);
}
free(dp);
See full program below
#include <stdio.h>
#include <stdlib.h>
void fun(int *ptr)
{
ptr[0] = 77;
ptr[1] = 88;
ptr[2] = 99;
}
int main(void)
{
int **dp;
dp = malloc(2 * sizeof(int *));
for (int i = 0; i < 2; i++)
{
dp[i] = malloc(3 * sizeof(int));
}
dp[0][0] = 11;
dp[0][1] = 22;
dp[0][2] = 33;
dp[1][0] = 44;
dp[1][1] = 55;
dp[1][2] = 66;
printf("----- Before Call By Reference -----\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
printf("dp[%d][%d] = %d\n", i, j, dp[i][j]);
}
}
// Method 1 : Access Single dimension arrays
fun( &dp[0][0] );
fun( &dp[1][0] );
// Method 2 : Access Single dimension arrays
fun( dp[0] );
fun( dp[1] );
// Method 3 : Access Single dimension arrays
fun( *dp );
fun( *(dp + 1) );
printf("----- After Call By Reference -----\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
printf("dp[%d][%d] = %d\n", i, j, dp[i][j]);
}
}
for (int i = 0; i < 2; i++)
{
free(dp[i]);
}
free(dp);
return 0;
}
Output is as below
----- Before Call By Reference -----
dp[0][0] = 11
dp[0][1] = 22
dp[0][2] = 33
dp[1][0] = 44
dp[1][1] = 55
dp[1][2] = 66
----- After Call By Reference -----
dp[0][0] = 77
dp[0][1] = 88
dp[0][2] = 99
dp[1][0] = 77
dp[1][1] = 88
dp[1][2] = 99
Step 1 : Consider a double dimension array created using a integer double pointer
int **dp;
dp = malloc(2 * sizeof(int *));
for (int i = 0; i < 2; i++)
{
dp[i] = malloc(3 * sizeof(int));
}
dp[0][0] = 11;
dp[0][1] = 22;
dp[0][2] = 33;
dp[1][0] = 44;
dp[1][1] = 55;
dp[1][2] = 66;
Step 2 : Pass Address of Double Dimension array to a function
fun(&dp);
Step 3.1 : Define function
fun
void fun(int ***ptr )
{
}
Step 3.2 : Access and change individual integers inside function
fun
int data = 99;
for (int i = 0 ; i < 2; i++) {
for (int j = 0 ; j < 3; j++) {
(*ptr)[i][j] = data++;
}
}
Step 4 : Free memory after usage
for (int i = 0; i < 2; i++)
{
free(dp[i]);
}
free(dp);
See full program below
#include <stdio.h>
#include <stdlib.h>
void fun(int ***ptr)
{
int data = 99;
for (int i = 0 ; i < 2; i++) {
for (int j = 0 ; j < 3; j++) {
(*ptr)[i][j] = data++;
}
}
}
int main(void)
{
int **dp;
dp = malloc(2 * sizeof(int *));
for (int i = 0; i < 2; i++)
{
dp[i] = malloc(3 * sizeof(int));
}
dp[0][0] = 11;
dp[0][1] = 22;
dp[0][2] = 33;
dp[1][0] = 44;
dp[1][1] = 55;
dp[1][2] = 66;
printf("----- Before Call By Reference -----\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
printf("dp[%d][%d] = %d\n", i, j, dp[i][j]);
}
}
fun(&dp);
printf("----- After Call By Reference -----\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
printf("dp[%d][%d] = %d\n", i, j, dp[i][j]);
}
}
for (int i = 0; i < 2; i++)
{
free(dp[i]);
}
free(dp);
return 0;
}
Output is as below
----- Before Call By Reference -----
dp[0][0] = 11
dp[0][1] = 22
dp[0][2] = 33
dp[1][0] = 44
dp[1][1] = 55
dp[1][2] = 66
----- After Call By Reference -----
dp[0][0] = 99
dp[0][1] = 100
dp[0][2] = 101
dp[1][0] = 102
dp[1][1] = 103
dp[1][2] = 104
Other topics of integer and functions
Current Module
Previous Module
Next Module
Other Modules