Malloc char Triple pointer

  • In this section, you are going to learn

How to allocate memory using malloc ?

How to allocate memory using calloc ?

How to allocate memory using realloc ?

  • Key point to remember

    • Triple pointer points to array of double pointers

    • Double pointer points to array of single pointers

    • Single pointer points to array of characters

  • Example is arr[2][3][4]

  • Now let us take a look at real time examples

  • Step 1 : Include stdlib.h

#include <stdlib.h>
  • Step 2 : Define Triple Pointer

char ***ptr;
  • Step 3.1 : ptr is a triple pointer and points to array of double pointers

ptr = malloc(sizeof(char **));
  • Step 3.2 : *ptr is a double pointer and points to array of single pointers

*ptr = malloc(sizeof(char *));
  • Step 3.3 : **ptr is a single pointer and points to array of characters

**ptr = malloc(sizeof(char));
  • Step 4 : Assign a value to character

***ptr = 65;
  • Step 5 : Print the value of character

printf("***ptr = %d\n", ***ptr);
  • Step 6.1 : Free array of characters

free(**ptr);
  • Step 6.2 : Free array of single pointers

free(*ptr);
  • Step 6.3 : Free array of double pointers

free(ptr);
  • See the full program below

 1#include <stdio.h>
 2
 3// Step 1 : Include stdlib.h
 4#include <stdlib.h>
 5
 6int main(void)
 7{
 8        // Step 2 : Define Triple Pointer
 9        char ***ptr;
10
11        // Step 3.1 : ptr is a triple pointer and points to array of double pointers
12        ptr = malloc(sizeof(char **));
13
14        // Step 3.2 : *ptr is a double pointer and points to array of single pointers
15        *ptr = malloc(sizeof(char *));
16
17        // Step 3.3 : **ptr is a single pointer and points to array of characters
18        **ptr = malloc(sizeof(char));
19
20        // Step 4 : Assign a value to character
21        ***ptr = 65;
22
23        // Step 5 : Print the value of character
24        printf("***ptr = %d\n", ***ptr);
25
26        // Step 6.1 : Free array of characters
27        free(**ptr);
28
29        // Step 6.2 : Free array of single pointers
30        free(*ptr);
31
32       // Step 6.3 : Free array of double pointers
33        free(ptr);
34
35        return 0;
36}

Statement

Description

ptr

  • ptr is a triple pointer

*ptr

  • *ptr is a double pointer

**ptr

  • **ptr is a single pointer

***ptr

  • ***ptr is a character

sizeof(ptr)

  • 8 Bytes

  • Not fully dereferenced

  • Hence ptr is a pointer

  • sizeof(pointer) is 8 Bytes always

sizeof(*ptr)

  • 8 Bytes

  • Not fully dereferenced

  • Hence *ptr is a pointer

  • sizeof(pointer) is 8 Bytes always

sizeof(**ptr)

  • 8 Bytes

  • Not fully dereferenced

  • Hence **ptr is a pointer

  • sizeof(pointer) is 8 Bytes always

sizeof(***ptr)

  • 1 Byte

  • Fully dereferenced

  • Hence ***ptr is a character

  • sizeof(character) is 1 Byte always

ptr = malloc( sizeof(char **) )

  • ptr points to array of double pointers

*ptr = malloc( sizeof(char *) )

  • *ptr points to array of single pointers

**ptr = malloc( sizeof(char ) )

  • **ptr points to array of characters

  • Step 1 : Include stdlib.h

#include <stdlib.h>
  • Step 2 : Define Triple Pointer

char ***ptr;
  • Step 3.1 : ptr is a triple pointer and points to array of double pointers

ptr = malloc(sizeof(char **));
  • Step 3.2 : *ptr is a double pointer and points to array of single pointers

*ptr = malloc(sizeof(char *));
  • Step 3.3 : **ptr is a single pointer and points to array of characters

**ptr = malloc(10 * sizeof(char));
  • Step 4 : **ptr is now pointing to array of 10 characters. Hence can use it for string operations

strcpy(**ptr, "Laptop");
  • Step 5 : Print the string

printf("**ptr = %s\n", **ptr);
  • Step 6.1 : Free array of characters

free(**ptr);
  • Step 6.2 : Free array of single pointers

free(*ptr);
  • Step 6.3 : Free array of double pointers

free(ptr);
  • See the full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4// Step 1 : Include stdlib.h
 5#include <stdlib.h>
 6
 7int main(void)
 8{
 9        // Step 2 : Define Triple Pointer
10        char ***ptr;
11
12        // Step 3.1 : ptr is a triple pointer and points to array of double pointers
13        ptr = malloc(sizeof(char **));
14
15        // Step 3.2 : *ptr is a double pointer and points to array of single pointers
16        *ptr = malloc(sizeof(char *));
17
18        // Step 3.3 : **ptr is a single pointer and points to array of characters
19        **ptr = malloc(10 * sizeof(char));
20
21        // Step 4 : **ptr is now pointing to array of 10 characters. Hence can use it for string operations
22        strcpy(**ptr, "Laptop");
23
24        // Step 5 : Print the string
25        printf("**ptr = %s\n", **ptr);
26
27        for (int i = 0; i < strlen(**ptr); i++)
28        {
29                printf("(**ptr)[i] = %c\n", (**ptr)[i]);
30        }
31
32        for (int i = 0; i < strlen(**ptr); i++)
33        {
34                printf("ptr[0][0][i] = %c\n", ptr[0][0][i]);
35        }
36
37        // Step 6.1 : Free array of characters
38        free(**ptr);
39
40        // Step 6.2 : Free array of single pointers
41        free(*ptr);
42
43        // Step 6.3 : Free array of double pointers
44        free(ptr);
45
46        return 0;
47}

Statement

Description

ptr

  • ptr is a triple pointer

*ptr

  • *ptr is a double pointer

**ptr

  • **ptr is a single pointer

***ptr

  • ***ptr is a character

sizeof(ptr)

  • 8 Bytes

  • Not fully dereferenced

  • Hence ptr is a pointer

  • sizeof(pointer) is 8 Bytes always

sizeof(*ptr)

  • 8 Bytes

  • Not fully dereferenced

  • Hence *ptr is a pointer

  • sizeof(pointer) is 8 Bytes always

sizeof(**ptr)

  • 8 Bytes

  • Not fully dereferenced

  • Hence **ptr is a pointer

  • sizeof(pointer) is 8 Bytes always

sizeof(***ptr)

  • 1 Byte

  • Fully dereferenced

  • Hence ***ptr is a character

  • sizeof(character) is 1 Byte always

ptr = malloc( sizeof(char **) )

  • ptr points to array of double pointers

*ptr = malloc( sizeof(char *) )

  • *ptr points to array of single pointers

**ptr = malloc( 10 * sizeof(char ) )

  • **ptr points to array of 10 characters

strcpy(**ptr, “Laptop”);

  • Copies string “Laptop” to *ptr

(**ptr)[i]

  • Method 1 : To access individual characters

ptr[0][0][i]

  • Method 2 : To access individual characters

  • Step 1 : Include stdlib.h

#include <stdlib.h>
  • Step 2 : Define Triple Pointer

char ***ptr;
  • Step 3.1 : ptr is a triple pointer and points to array of double pointers

ptr = malloc(sizeof(char **));
  • Step 3.2 : Create array of single pointers from (*ptr)[0](*ptr)[9]

*ptr = malloc(10 * sizeof(char *));
  • Step 3.3 : Now create array of characters for every single pointer from (*ptr)[0](*ptr)[9]

for (int i = 0; i < 10; i++) {
        (*ptr)[i] = malloc(10 * sizeof(char));
}
  • Step 4 : (*ptr)[0] .. (*ptr)[9] is now pointing to array of characters each

for (int i = 0; i < 10; i++) {
        sprintf((*ptr)[i], "Laptop %d", i);
}
  • Step 5 : Print the strings

for (int i = 0; i < 10; i++) {
        printf("(*ptr)[i] = %s\n", (*ptr)[i]);
}
  • Step 6.1 : Free array of characters pointed by all single pointers

for (int i = 0; i < 10; i++) {
        free((*ptr)[i]);
}
  • Step 6.2 : Free array of single pointers

free(*ptr);
  • Step 6.3 : Free array of double pointers

free(ptr);
  • See the full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4// Step 1 : Include stdlib.h
 5#include <stdlib.h>
 6
 7int main(void)
 8{
 9        // Step 2 : Define Triple Pointer
10        char ***ptr;
11
12        // Step 3.1 : ptr is a triple pointer and points to array of double pointers
13        ptr = malloc(sizeof(char **));
14
15        // Step 3.2
16        // *ptr is a double pointer and points to array of single pointers
17        // Create array of single pointers from (*ptr)[0] ... (*ptr)[9]
18        *ptr = malloc(10 * sizeof(char *));
19
20        // Step 3.3 : Now create array of characters for every single pointer from (*ptr)[0] ... (*ptr)[9]
21
22        for (int i = 0; i < 10; i++) {
23                (*ptr)[i] = malloc(10 * sizeof(char));
24        }
25
26        // Step 4 : (*ptr)[0] .. (*ptr)[9] is now pointing to array of characters each
27        for (int i = 0; i < 10; i++) {
28                sprintf((*ptr)[i], "Laptop %d", i);
29        }
30
31        // Step 5 : Print the strings
32        for (int i = 0; i < 10; i++) {
33                printf("(*ptr)[i] = %s\n", (*ptr)[i]);
34        }
35
36        // Step 6.1 : Free array of characters pointed by all single pointers
37        for (int i = 0; i < 10; i++) {
38                free((*ptr)[i]);
39        }
40
41        // Step 6.2 : Free array of single pointers
42        free(*ptr);
43
44        // Step 6.3 : Free array of double pointers
45        free(ptr);
46
47        return 0;
48}
  • Step 1 : Include stdlib.h

#include <stdlib.h>
  • Step 2 : Define Triple Pointer

char ***ptr;
  • Step 3.1 : Create array of double pointers from ptr[0] … ptr[9]

ptr = malloc(10 * sizeof(char **));
  • Step 3.2 : Create array of single pointers for every double pointer

for (int i = 0; i < 10; i++) {
        ptr[i] = malloc(10 * sizeof(char *));
}
  • Step 3.3 : Create array of characters for every single pointer from ptr[0][9] … ptr[9][9]

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
                ptr[i][j] = malloc(10 * sizeof(char));
        }
}
  • Step 4 : Use ptr[0][0] … ptr[9][9] as character array

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
                sprintf(ptr[i][j], "Laptop%d%d", i,j);
        }
}
  • Step 5 : Print the strings

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
                printf("ptr[%d][%d] = %s\n", i, j, ptr[i][j]);
        }
}
  • Step 6.1 : Free array of characters pointed by all single pointers

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
                free(ptr[i][j]);
        }
}
  • Step 6.2 : Free array of single pointers

for (int i = 0; i < 10; i++) {
        free(ptr[i]);
}
  • Step 6.3 : Free array of double pointers

free(ptr);
  • See the full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4// Step 1 : Include stdlib.h
 5#include <stdlib.h>
 6
 7int main(void)
 8{
 9        // Step 2 : Define Triple Pointer
10        char ***ptr;
11
12        // Step 3.1 : Create array of double pointers from ptr[0] ... ptr[9]
13        ptr = malloc(10 * sizeof(char **));
14
15        // Step 3.2 : Create array of single pointers for every double pointer
16        for (int i = 0; i < 10; i++) {
17                ptr[i] = malloc(10 * sizeof(char *));
18        }
19
20        // Step 3.3 : Create array of characters for every single pointer from ptr[0][9] ... ptr[9][9]
21        for (int i = 0; i < 10; i++) {
22                for (int j = 0; j < 10; j++) {
23                        ptr[i][j] = malloc(10 * sizeof(char));
24                }
25        }
26
27        // Step 4 : Use ptr[0][0] ... ptr[9][9] as character array
28        for (int i = 0; i < 10; i++) {
29                for (int j = 0; j < 10; j++) {
30                        sprintf(ptr[i][j], "Laptop%d%d", i,j);
31                }
32        }
33
34        // Step 5 : Print the strings
35        for (int i = 0; i < 10; i++) {
36                for (int j = 0; j < 10; j++) {
37                        printf("ptr[%d][%d] = %s\n", i, j, ptr[i][j]);
38                }
39        }
40
41        // Step 6.1 : Free array of characters pointed by all single pointers
42        for (int i = 0; i < 10; i++) {
43                for (int j = 0; j < 10; j++) {
44                        free(ptr[i][j]);
45                }
46        }
47
48        // Step 6.2 : Free array of single pointers
49        for (int i = 0; i < 10; i++) {
50                free(ptr[i]);
51        }
52
53        // Step 6.3 : Free array of double pointers
54        free(ptr);
55
56        return 0;
57}
  • Step 1 : Include stdlib.h

#include <stdlib.h>
  • Step 2 : Define an allocation function

int get_memory(char ****fp)
{
        return 0;
}
  • Step 3.1 : Create array of double pointers from (*fp)[0](*fp)[9]

int get_memory(char ****fp)
{
        // Step 3.1 : Create array of double pointers from (*fp)[0] ... (*fp)[9]
        *fp = malloc(10 * sizeof(char **));

        return 0;
}
  • Step 3.2 : Create array of single pointers for every double pointer

int get_memory(char ****fp)
{
        // Step 3.1 : Create array of double pointers from (*fp)[0] ... (*fp)[9]
        *fp = malloc(10 * sizeof(char **));

        // Step 3.2 : Create array of single pointers for every double pointer
        for (int i = 0; i < 10; i++) {
                (*fp)[i] = malloc(10 * sizeof(char *));
        }

        return 0;
}
  • Step 3.3 : Create array of characters for every single pointer from (*fp)[0][9](*fp)[9][9]

int get_memory(char ****fp)
{
        // Step 3.1 : Create array of double pointers from (*fp)[0] ... (*fp)[9]
        *fp = malloc(10 * sizeof(char **));

        // Step 3.2 : Create array of single pointers for every double pointer
        for (int i = 0; i < 10; i++) {
                (*fp)[i] = malloc(10 * sizeof(char *));
        }

        // Step 3.3 : Create array of characters for every single pointer from (*fp)[0][9] ... (*fp)[9][9]
        for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 10; j++) {
                        (*fp)[i][j] = malloc(10 * sizeof(char));
                }
        }

        return 0;
}
  • Step 4 : Define Triple Pointer in caller

char ***tp;
  • Step 5 : Pass Triple pointer as reference

get_memory(&tp);
  • Step 6 : Use tp[0][0] … tp[9][9] as character array

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
                sprintf(tp[i][j], "Laptop%d%d", i,j);
        }
}
  • Step 7 : Print the strings

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
                printf("tp[%d][%d] = %s\n", i, j, tp[i][j]);
        }
}
  • Step 6.1 : Free array of characters pointed by all single pointers

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
                free(tp[i][j]);
        }
}
  • Step 6.2 : Free array of single pointers

for (int i = 0; i < 10; i++) {
        free(tp[i]);
}
  • Step 6.3 : Free array of double pointers

free(tp);
  • See the full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4// Step 1 : Include stdlib.h
 5#include <stdlib.h>
 6
 7int get_memory(char ****fp)
 8{
 9        // Step 3.1 : Create array of double pointers from (*fp)[0] ... (*fp)[9]
10        *fp = malloc(10 * sizeof(char **));
11
12        // Step 3.2 : Create array of single pointers for every double pointer
13        for (int i = 0; i < 10; i++) {
14                (*fp)[i] = malloc(10 * sizeof(char *));
15        }
16
17        // Step 3.3 : Create array of characters for every single pointer from (*fp)[0][9] ... (*fp)[9][9]
18        for (int i = 0; i < 10; i++) {
19                for (int j = 0; j < 10; j++) {
20                        (*fp)[i][j] = malloc(10 * sizeof(char));
21                }
22        }
23
24        return 0;
25}
26
27int main(void)
28{
29        // Step 2 : Define Triple Pointer
30        char ***tp;
31
32        // Step 3 : Pass Triple pointer as reference
33        get_memory(&tp);
34
35        // Step 4 : Use tp[0][0] ... tp[9][9] as character array
36        for (int i = 0; i < 10; i++) {
37                for (int j = 0; j < 10; j++) {
38                        sprintf(tp[i][j], "Laptop%d%d", i,j);
39                }
40        }
41
42        // Step 5 : Print the strings
43        for (int i = 0; i < 10; i++) {
44                for (int j = 0; j < 10; j++) {
45                        printf("tp[%d][%d] = %s\n", i, j, tp[i][j]);
46                }
47        }
48
49        // Step 6.1 : Free array of characters pointed by all single pointers
50        for (int i = 0; i < 10; i++) {
51                for (int j = 0; j < 10; j++) {
52                        free(tp[i][j]);
53                }
54        }
55
56        // Step 6.2 : Free array of single pointers
57        for (int i = 0; i < 10; i++) {
58                free(tp[i]);
59        }
60
61        // Step 6.3 : Free array of double pointers
62        free(tp);
63
64        return 0;
65}