Basics of Character Triple Pointers
In this section, you are going to learn
How to use Triple Pointers ?
Basics of Triple Pointers
1 Triple Pointer 1 Double Pointer, 1 Single Pointer : Simple variables
1 Triple Pointer, 1 Double Pointer, 1 Single Pointer : With Single pointer pointing to arrays
1 Triple Pointer, 1 Double Pointer, 1 Single Pointer : With Single pointer heap allocation
1 Triple Pointer, 1 Double Pointer : With two heap allocations : Create 1x1 array
1 Triple Pointer, 1 Double Pointer : With two heap allocations : Create 1x10 array
1 Triple Pointer, 1 Double Pointer : Double Pointer Pointing to array of single pointers : Static
1 Triple Pointer, 1 Double Pointer : Pointing to array of single pointers : Dynamic
Step 1 : Define a Character
char c = 65;
Step 2 : Define a Single pointer
char *sp = &c;
OR
char *sp;
sp = &c;
Step 3 : Define a Double pointer
char **dp = &sp;
OR
char **dp;
dp = &sp;
Step 4 : Define a Triple pointer
char ***tp = &dp;
OR
char ***tp;
tp = &dp;
Step 5 : Access user data (in this case character) using single pointer
printf("*sp = %d\n", *sp);
Step 6 : Access user data (in this case character) using double pointer
printf("**dp = %d\n", **dp);
Step 7 : Access user data (in this case character) using triple pointer
printf("***tp = %d\n", ***tp);
Step 8 : Use **tp to point to new user data (in this case variable d)
**tp = &d;
Step 9 : Now user data can be accessed using *sp, **dp, ***tp which prints value of character d
printf("d = %d\n", d);
printf("d = %d\n", *sp);
printf("d = %d\n", **dp);
printf("d = %d\n", ***tp);
See full program below
#include <stdio.h>
int main(void)
{
char c = 65;
char *sp = &c;
char **dp = &sp;
char ***tp = &dp;
printf("c = %d\n", c);
printf("*sp = %d\n", *sp);
printf("**dp = %d\n", **dp);
printf("***tp = %d\n", ***tp);
char d = 100;
**tp = &d;
printf("d = %d\n", d);
printf("*sp = %d\n", *sp);
printf("**dp = %d\n", **dp);
printf("***tp = %d\n", ***tp);
return 0;
}
Output is as below
c = 65
*sp = 65
**dp = 65
***tp = 65
d = 100
*sp = 100
**dp = 100
***tp = 100
Summary of Naming conventions
Consider
char ***tp;
then
tp is a triple pointer
*tp is a double pointer
**tp is a single pointer
***tp is user data
Step 1 : Define a single dimension array of characters
char arr[] = "Laptop";
Step 2 : Define a single pointer
char *sp = arr;
OR
char *sp;
sp = arr;
OR
char *sp;
sp = &arr[0];
Step 3 : Define a double pointer
char **dp = &sp;
OR
char **dp;
dp = &sp;
Step 4 : Define a Triple pointer
char ***tp = &dp;
OR
char ***tp;
tp = &dp;
Step 5 : Access user data (in this case array of characters) using single pointer variable
sp
printf("arr = %s\n", sp);
Step 6 : Access user data (in this case array of characters) using double pointer variable
dp
printf("arr = %s\n", *dp);
Step 7 : Access user data (in this case array of characters) using triple pointer variable
tp
printf("arr = %s\n", **tp);
Note *dp
should be called as single pointer because of below equations
dp = &sp;
*dp = sp;
Note **tp
should be called as single pointer because of below equations
tp = &dp;
*tp = dp;
*tp = &sp;
**tp = sp;
See full program below
#include <stdio.h>
int main(void)
{
char arr[] = "Laptop";
char *sp = arr;
char **dp = &sp;
char ***tp = &dp;
//Access full array
printf("arr = %s\n", arr);
printf("sp = %s\n", sp);
printf("*dp = %s\n", *dp);
printf("**tp = %s\n", **tp);
//Access individual character
printf("arr[3] = %c\n", arr[3]);
printf("sp[3] = %c\n", sp[3]);
printf("(*dp)[3] = %c\n", (*dp)[3]);
printf("(**tp)[3] = %c\n", (**tp)[3]);
return 0;
}
Output is as below
arr = Laptop
sp = Laptop
*dp = Laptop
**tp = Laptop
arr[3] = t
sp[3] = t
(*dp)[3] = t
(**tp)[3] = t
Step 1 : Define a single pointer
char *sp;
Step 2 : Allocate heap memory to single pointer
sp = malloc(10 * sizeof(char));
Step 3 : Copy User data to heap memory
strcpy(sp, "Laptop");
Step 4 : Define a double pointer
char **dp;
dp = &sp;
Step 5 : Define a Triple pointer
char ***tp = &dp;
OR
char ***tp;
tp = &dp;
Step 6 : Access User data using single pointer variable
sp
printf("sp = %s\n", sp);
printf("sp[3] = %c\n", sp[3]);
Step 7 : Access User data using double pointer variable
dp
printf("*dp = %s\n", *dp);
printf("(*dp)[3] = %c\n", (*dp)[3]);
Step 8 : Access User data using triple pointer variable
tp
printf("**tp = %s\n", **tp);
printf("(**tp)[3] = %c\n", (**tp)[3]);
Step 9 : Free memory after usage
free(sp);
OR
free(*dp);
OR
free(**tp);
See full program below
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *sp;
sp = malloc(10 * sizeof(char));
strcpy(sp, "Laptop");
char **dp;
dp = &sp;
char ***tp;
tp = &dp;
printf("sp = %s\n", sp);
printf("*dp = %s\n", *dp);
printf("**tp = %s\n", **tp);
//Access individual character
printf("sp[3] = %c\n", sp[3]);
printf("(*dp)[3] = %c\n", (*dp)[3]);
printf("(**tp)[3] = %c\n", (**tp)[3]);
free(sp);
return 0;
}
Output is as below
sp = Laptop
*dp = Laptop
**tp = Laptop
sp[3] = t
(*dp)[3] = t
(**tp)[3] = t
Step 1 : Define a double pointer
char **dp;
Step 2 : Allocate memory to a double pointer
dp = malloc(sizeof(char *));
Step 3 : Allocate memory to a single pointer
*dp = malloc(sizeof(char));
Step 4 : Store user data
**dp = 65;
Step 5 : Define a Triple pointer
char ***tp = &dp;
OR
char ***tp;
tp = &dp;
Step 6 : Read user data using
dp
printf("**dp = %d\n", **dp);
Step 7 : Read user data using
tp
printf("***tp = %d\n", ***tp);
Step 8 : Free memory in opposite flow of allocation
free(*dp);
free(dp);
OR
free(**tp);
free(*tp);
See full program below
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char **dp;
dp = malloc(sizeof(char *));
*dp = malloc(sizeof(char));
**dp = 65;
char ***tp;
tp = &dp;
printf("**dp = %d\n", **dp);
printf("***tp = %d\n", ***tp);
free(*dp); // or free(**tp);
free(dp); // or free(*tp);
return 0;
}
Output is as below
**dp = 65
***tp = 65
Step 1 : Define a double pointer
char **dp;
Step 2 : Allocate memory to a double pointer
dp = malloc(sizeof(char *));
Step 3 : Allocate memory to a single pointer
*dp = malloc(10 * sizeof(char));
Step 4 : Copy User data to heap
strcpy(*dp, "Laptop123");
Step 5 : Define a Triple pointer
char ***tp = &dp;
OR
char ***tp;
tp = &dp;
Step 6 : Read user data from heap using
dp
printf("*dp = %s\n", *dp);
for (int i = 0; i < 10; i++) {
printf("(*dp)[%d] = %c\n", i, (*dp)[i]);
}
Step 7 : Read user data from heap using
tp
printf("**tp = %s\n", **tp);
for (int i = 0; i < 10; i++) {
printf("(**tp)[%d] = %c\n", i, (**tp)[i]);
}
Step 6 : Free memory in opposite flow of allocation
free(*dp);
free(dp);
OR
free(**tp);
free(*tp);
See full program below
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char **dp;
dp = malloc(sizeof(char *));
*dp = malloc(10 * sizeof(char));
memset(*dp, 0, 10);
strcpy(*dp, "Laptop123");
char ***tp;
tp = &dp;
printf("*dp = %s\n", *dp);
for (int i = 0; i < 10; i++) {
printf("(*dp)[%d] = %c\n", i, (*dp)[i]);
}
printf("**tp = %s\n", **tp);
for (int i = 0; i < 10; i++) {
printf("(**tp)[%d] = %c\n", i, (**tp)[i]);
}
free(*dp); // or free(**tp);
free(dp); // or free(*tp);
return 0;
}
Output is as below
*dp = Laptop123
(*dp)[0] = L
(*dp)[1] = a
(*dp)[2] = p
(*dp)[3] = t
(*dp)[4] = o
(*dp)[5] = p
(*dp)[6] = 1
(*dp)[7] = 2
(*dp)[8] = 3
(*dp)[9] =
**tp = Laptop123
(**tp)[0] = L
(**tp)[1] = a
(**tp)[2] = p
(**tp)[3] = t
(**tp)[4] = o
(**tp)[5] = p
(**tp)[6] = 1
(**tp)[7] = 2
(**tp)[8] = 3
(**tp)[9] =
Step 1 : Define 3 Single dimension character arrays
char arr0[32] = "Laptop";
char arr1[32] = "Mouse";
char arr2[32] = "Keyboard";
Step 2 : Define array of single pointers
char *sp_arr[] = {arr0, arr1, arr2};
Step 3 : Define a double pointer
char **dp;
dp = sp_arr;
Step 4 : Use
dp
to change contents of single dimension arrays
strcpy(dp[0], "New Laptop");
strcpy(dp[1], "New Mouse");
strcpy(dp[2], "New Keyboard");
Step 5 : Define a Triple pointer
char ***tp = &dp;
OR
char ***tp;
tp = &dp;
Step 6 : Use
sp_arr
to access contents of single dimension arrays
printf("sp_arr = %s\n", sp_arr[0]);
printf("sp_arr = %s\n", sp_arr[1]);
printf("sp_arr = %s\n", sp_arr[2]);
Step 7 : Use
dp
to access contents of single dimension arrays
printf("dp[0] = %s\n", dp[0]);
printf("dp[1] = %s\n", dp[1]);
printf("dp[2] = %s\n", dp[2]);
Step 8 : Use
tp
to access contents of single dimension arrays
printf("(*tp)[0] = %s\n", (*tp)[0]);
printf("(*tp)[1] = %s\n", (*tp)[1]);
printf("(*tp)[2] = %s\n", (*tp)[2]);
See full program below
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char arr0[32] = "Laptop";
char arr1[32] = "Mouse";
char arr2[32] = "Keyboard";
char *sp_arr[] = {arr0, arr1, arr2};
char **dp;
dp = sp_arr;
char ***tp;
tp = &dp;
strcpy(dp[0], "New Laptop");
strcpy(dp[1], "New Mouse");
strcpy(dp[2], "New Keyboard");
printf("sp_arr = %s\n", sp_arr[0]);
printf("sp_arr = %s\n", sp_arr[1]);
printf("sp_arr = %s\n", sp_arr[2]);
printf("dp[0] = %s\n", dp[0]);
printf("dp[1] = %s\n", dp[1]);
printf("dp[2] = %s\n", dp[2]);
printf("(*tp)[0] = %s\n", (*tp)[0]);
printf("(*tp)[1] = %s\n", (*tp)[1]);
printf("(*tp)[2] = %s\n", (*tp)[2]);
return 0;
}
Output is as below
sp_arr = New Laptop
sp_arr = New Mouse
sp_arr = New Keyboard
dp[0] = New Laptop
dp[1] = New Mouse
dp[2] = New Keyboard
(*tp)[0] = New Laptop
(*tp)[1] = New Mouse
(*tp)[2] = New Keyboard
Step 1 : Define a double pointer
char **dp;
Step 2 : Allocate heap memory : Create 3 single pointers
dp[0]
,dp[1]
,dp[2]
dp = malloc(3 * sizeof(char *));
Step 3 : Allocate heap memory : Create 3 single dimension character arrays of size 32 characters each
dp[0] = malloc(32 * sizeof(char));
dp[1] = malloc(32 * sizeof(char));
dp[2] = malloc(32 * sizeof(char));
Step 4 : Store user data
strcpy(dp[0], "New Laptop");
strcpy(dp[1], "New Mouse");
strcpy(dp[2], "New Keyboard");
Step 5 : Define a Triple pointer
char ***tp = &dp;
OR
char ***tp;
tp = &dp;
Step 6 : Access user data using
dp
printf("dp[0] = %s\n", dp[0]);
printf("dp[1] = %s\n", dp[1]);
printf("dp[2] = %s\n", dp[2]);
Step 7 : Access user data using
tp
printf("(*tp)[0] = %s\n", (*tp)[0]);
printf("(*tp)[1] = %s\n", (*tp)[1]);
printf("(*tp)[2] = %s\n", (*tp)[2]);
Step 6 : Free 3 character arrays
free(dp[0]);
free(dp[1]);
free(dp[2]);
OR
free((*tp)[0]);
free((*tp)[1]);
free((*tp)[2]);
Step 7 : Free 3 single pointers
free(dp);
See full program below
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char **dp;
dp = malloc(3 * sizeof(char *));
dp[0] = malloc(32 * sizeof(char));
dp[1] = malloc(32 * sizeof(char));
dp[2] = malloc(32 * sizeof(char));
strcpy(dp[0], "New Laptop");
strcpy(dp[1], "New Mouse");
strcpy(dp[2], "New Keyboard");
char ***tp;
tp = &dp;
printf("dp[0] = %s\n", dp[0]);
printf("dp[1] = %s\n", dp[1]);
printf("dp[2] = %s\n", dp[2]);
printf("(*tp)[0] = %s\n", (*tp)[0]);
printf("(*tp)[1] = %s\n", (*tp)[1]);
printf("(*tp)[2] = %s\n", (*tp)[2]);
free(dp[0]);
free(dp[1]);
free(dp[2]);
free(dp);
return 0;
}
Output as is below
dp[0] = New Laptop
dp[1] = New Mouse
dp[2] = New Keyboard
(*tp)[0] = New Laptop
(*tp)[1] = New Mouse
(*tp)[2] = New Keyboard
Current Module
Previous Module
Next Module
Other Modules