Malloc char Double 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
Double pointer points to array of single pointers
Single pointer points to array of characters
Example is arr[2][3]
Where there are 2 rows and 3 columns
Now let us take a look at real time examples
Step 1 : Include stdlib.h
#include <stdlib.h>
Step 2 : Define Double pointer
char **ptr;
Step 3 : Create One single pointer
ptr = malloc( sizeof(char *) );
Allocates 8 Bytes of memory in heap
ptr
points to array of single pointersIn this case, array has one single pointer
sizeof(ptr) is 8 Bytes
Step 4 : Create One single character
*ptr = malloc( sizeof(char) );
OR
ptr[0] = malloc( sizeof(char) );
Allocates 1 Byte of memory in heap
Step 5 : Assign value to character
**ptr = 65;
OR
*ptr[0] = 65;
OR
ptr[0][0] = 65;
Step 6 : Print the value
printf("**ptr = %d\n", **ptr);
printf("*ptr[0] = %d\n", *ptr[0]);
printf("ptr[0][0] = %d\n", ptr[0][0]);
Step 7 : Free after use
free(*ptr);
free(ptr);
Free in the opposite order of allocation
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 Double pointer
9 char **ptr;
10
11 // Step 3 : Create One single pointer
12 ptr = malloc( sizeof(char *) );
13
14 // Step 4 : Create One single character
15 *ptr = malloc( sizeof(char) );
16
17 // Step 5 : Assign value to character
18 **ptr = 65;
19
20 // Step 6 : Print the value
21 printf("**ptr = %d\n", **ptr);
22
23 // Step 7 : Free after use
24 free(*ptr);
25 free(ptr);
26
27 return 0;
28}
Statement |
Description |
---|---|
ptr = malloc( sizeof(char *) ) |
|
*ptr = malloc( sizeof(char ) ) |
|
sizeof(ptr) |
|
sizeof(*ptr) |
|
sizeof(**ptr) |
|
Step 1 : Include stdlib.h
#include <stdlib.h>
Step 2 : Define Double pointer
char **ptr;
Step 3 : Create One single pointer
ptr = malloc( sizeof(char *) );
Allocates 8 Bytes of memory in heap
ptr
points to array of single pointersIn this case, array has one single pointer
sizeof(ptr) is 8 Bytes
Step 4 : Create Ten characters
*ptr = malloc( 10 * sizeof(char) );
OR
ptr[0] = malloc( 10 * sizeof(char) );
Allocates 10 Bytes of memory in heap
Step 5 : *ptr can be used as an array of characters now !
memset(*ptr, 0, 10);
strcpy(*ptr, "Laptop");
Step 6 : Print the string
printf("*ptr = %s\n", *ptr);
Step 7 : Print individual characters : Using (*ptr)[i]
for (int i = 0; i < 10; i++)
{
printf("%c", (*ptr)[i]);
}
Step 8 : Print individual characters : Using ptr[0][i]
for (int i = 0; i < 10; i++)
{
printf("%c", ptr[0][i]);
}
Step 9 : Free after use
free(*ptr);
free(ptr);
Free in the opposite order of allocation
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 Double pointer
10 char **ptr;
11
12 // Step 3 : Create One single pointer
13 ptr = malloc( sizeof(char *) );
14
15 // Step 4 : Create Ten characters
16 *ptr = malloc( 10 * sizeof(char) );
17
18 // Step 5 : *ptr is now pointing to array of chracters
19 memset(*ptr, 0, 10);
20 strcpy(*ptr, "Laptop");
21
22 // Step 6 : Print the string using %s
23 printf("\nMethod 1 : Print all characters using *ptr\n");
24 printf("*ptr = %s\n", *ptr);
25
26 // Step 7 : Print individual characters : Using (*ptr)[i]
27 printf("\nMethod 2 : Print characters using (*ptr)[i]\n");
28 for (int i = 0; i < 10; i++)
29 {
30 printf("%c", (*ptr)[i]);
31 }
32
33 // Step 8 : Print individual characters : Using ptr[0][i]
34 printf("\nMethod 2 : Print characters using ptr[0][i]\n");
35 for (int i = 0; i < 10; i++)
36 {
37 printf("%c", ptr[0][i]);
38 }
39
40 // Step 9 : Free after use
41 free(*ptr);
42 free(ptr);
43
44 return 0;
45}
Statement |
Description |
---|---|
ptr = malloc( sizeof(char *) ) |
|
*ptr = malloc( 10 * sizeof(char) ) |
|
sizeof(ptr) |
|
sizeof(*ptr) |
|
sizeof(**ptr) |
|
*ptr |
|
strcpy(*ptr, “Laptop”); |
|
(*ptr)[i] |
|
ptr[0][i] |
|
Step 1 : Include stdlib.h
#include <stdlib.h>
Step 2 : Define Double pointer
char **ptr;
Step 3 : Create Ten Single Pointers from ptr[0] … ptr[9]
ptr = malloc( 10 * sizeof(char *) );
Step 4 : Create One Character in every Row
for (int i = 0; i < 10; i++)
{
ptr[i] = malloc( sizeof(char) );
}
Step 5 : Assign values to characters
for (int i = 0; i < 10; i++)
{
ptr[i][0] = ++val;
}
Step 6 : Print the values
for (int i = 0; i < 10; i++)
{
printf("Row %d Column 0 : %d\n", i, ptr[i][0]);
}
Step 7 : Free after use
for (int i = 0; i < 10; i++)
{
free(ptr[i]);
}
free(ptr);
See the full program below
1#include <string.h>
2
3// Step 1 : Include stdlib.h
4#include <stdlib.h>
5
6int main(void)
7{
8 // Step 2 : Define Double pointer
9 char **ptr;
10 int val = 0;
11
12 // Step 3 : Create Ten Single Pointers from ptr[0] ... ptr[9]
13 ptr = malloc( 10 * sizeof(char *) );
14
15 // Step 4 : Create One Character in every Row
16 for (int i = 0; i < 10; i++)
17 {
18 ptr[i] = malloc( sizeof(char) );
19 }
20
21 // Step 5 : Assign values to characters
22 for (int i = 0; i < 10; i++)
23 {
24 ptr[i][0] = ++val;
25 }
26
27 // Step 6 : Print the values
28 for (int i = 0; i < 10; i++)
29 {
30 printf("Row %d Column 0 : %d\n", i, ptr[i][0]);
31 }
32
33 // Step 7 : Free after use
34 for (int i = 0; i < 10; i++)
35 {
36 free(ptr[i]);
37 }
38
39 free(ptr);
40
41 return 0;
42}
Step 1 : Include stdlib.h
#include <stdlib.h>
Step 2 : Define Double pointer
char **ptr;
Step 3 : Create Ten Single Pointers from ptr[0] … ptr[9]
ptr = malloc( 10 * sizeof(char *) );
Step 4 : Create Ten Characters in every Row
for (int i = 0; i < 10; i++)
{
ptr[i] = malloc( 10 * sizeof(char) );
}
Step 5 : Assign strings to array of characters
for (int i = 0; i < 10; i++)
{
sprintf(ptr[i], "Laptop %d", ++val);
}
Step 6 : Print the strings
for (int i = 0; i < 10; i++)
{
printf("Row %d string : %s\n", i, ptr[i]);
}
Step 7 : Free after use
for (int i = 0; i < 10; i++)
{
free(ptr[i]);
}
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 Double pointer
10 char **ptr;
11 int val = 0;
12
13 // Step 3 : Create Ten Single Pointers from ptr[0] ... ptr[9]
14 ptr = malloc( 10 * sizeof(char *) );
15
16 // Step 4 : Create Ten Characters in every Row
17 for (int i = 0; i < 10; i++)
18 {
19 ptr[i] = malloc( 10 * sizeof(char) );
20 }
21
22 //ptr[0] is an array of 10 characters. Same is true for ptr[1] to ptr[9]
23
24 // Step 5 : Assign strings to array of characters
25 for (int i = 0; i < 10; i++)
26 {
27 sprintf(ptr[i], "Laptop %d", ++val);
28 }
29
30 // Step 6 : Print the strings
31 for (int i = 0; i < 10; i++)
32 {
33 printf("Row %d string : %s\n", i, ptr[i]);
34 }
35
36 // Step 7 : Free after use
37 for (int i = 0; i < 10; i++)
38 {
39 free(ptr[i]);
40 }
41
42 free(ptr);
43
44 return 0;
45}
Example is
arr
has 5 rows, andarr[0] points to an array of 7 characters
arr[1] points to an array of 5 characters
arr[2] points to an array of 4 characters
arr[3] points to an array of 9 characters
arr[4] points to an array of 3 characters
Step 1 : Include stdlib.h
#include <stdlib.h>
Step 2 : Define Double pointer
char **arr;
Step 3 : Create Five Single Pointers from arr[0] … arr[4]
arr = malloc( 5 * sizeof(char *) );
Step 4 : Create Character arrays in every Row
arr[0] = malloc( 7 * sizeof(char) );
arr[1] = malloc( 5 * sizeof(char) );
arr[2] = malloc( 4 * sizeof(char) );
arr[3] = malloc( 9 * sizeof(char) );
arr[4] = malloc( 3 * sizeof(char) );
Step 5 : Copy strings to array along with ‘\0’
sprintf(arr[0], "Adam12");
sprintf(arr[1], "John");
sprintf(arr[2], "abc");
sprintf(arr[3], "12345678");
sprintf(arr[4], "Hi");
Step 6 : Print the strings
for (int i = 0; i < 5; i++)
{
printf("Row %d string : %s\n", i, arr[i]);
}
Step 7 : Free after use
for (int i = 0; i < 5; i++)
{
free(arr[i]);
}
free(arr);
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 Double pointer
10 char **arr;
11
12 // Step 3 : Create Five Single Pointers from arr[0] ... arr[4]
13 arr = malloc( 5 * sizeof(char *) );
14
15 // Step 4 : Create Character arrays in every Row
16 arr[0] = malloc( 7 * sizeof(char) );
17 arr[1] = malloc( 5 * sizeof(char) );
18 arr[2] = malloc( 4 * sizeof(char) );
19 arr[3] = malloc( 9 * sizeof(char) );
20 arr[4] = malloc( 3 * sizeof(char) );
21
22 // Step 5 : Copy strings to array along with '\0'
23 sprintf(arr[0], "Adam12");
24 sprintf(arr[1], "John");
25 sprintf(arr[2], "abc");
26 sprintf(arr[3], "12345678");
27 sprintf(arr[4], "Hi");
28
29 // Step 6 : Print the strings
30 for (int i = 0; i < 5; i++)
31 {
32 printf("Row %d string : %s\n", i, arr[i]);
33 }
34
35 // Step 7 : Free after use
36 for (int i = 0; i < 5; i++)
37 {
38 free(arr[i]);
39 }
40
41 free(arr);
42
43 return 0;
44}
Step 1 : Include stdlib.h
#include <stdlib.h>
Step 2 : Define an allocation function
void get_memory(char **dp)
{
// *dp is equal to sp in caller
*dp = malloc(10 * sizeof(char));
memset(*dp, 0, 10);
}
Step 3: Define a single pointer
char *sp;
Step 4 : Pass single pointer
sp
by reference
get_memory(&sp);
Step 5 :
sp
is now pointing to array of 10 characters
strcpy(sp, "Laptop 12");
Step 6 : Free the memory
free(sp);
See full program below
1#include <stdio.h>
2#include <string.h>
3
4// Step 1 : Include stdlib.h
5#include <stdlib.h>
6
7// Step 2 : Define an allocation function
8void get_memory(char **dp)
9{
10 // *dp is equal to sp in caller
11 *dp = malloc(10 * sizeof(char));
12
13 memset(*dp, 0, 10);
14}
15
16int main(void)
17{
18 // Step 3: Define a single pointer
19 char *sp;
20
21 // Step 4 : Pass single pointer "sp" by reference
22 get_memory(&sp);
23
24 // Step 5 : "sp" is now pointing to array of 10 characters
25 strcpy(sp, "Laptop 12");
26
27 printf("String is %s\n", sp);
28
29 // Step 6 : Free the memory
30 free(sp);
31
32 return 0;
33}
Step 1 : Include stdlib.h
#include <stdlib.h>
Step 2 : Define an allocation function
void get_memory(char ***tp)
{
}
Step 2.1 : Create Ten Single Pointers from tp[0] … tp[9]
void get_memory(char ***tp)
{
*tp = malloc( 10 * sizeof(char *) );
}
tp
is a triple pointer*tp
is a double pointer and will point to array of single pointers !
Step 2.2 : Create Ten Characters in every Row
void get_memory(char ***tp)
{
*tp = malloc( 10 * sizeof(char *) );
for (int i = 0; i < 10; i++)
{
(*tp)[i] = malloc( 10 * sizeof(char) );
}
}
*tp[]
is a single pointer and will point to array of characters
Step 3: Define a single pointer
char **dp;
Step 4 : Pass double pointer “dp” by reference
get_memory(&dp);
Step 5 : Assign strings to array of characters
for (int i = 0; i < 10; i++)
{
sprintf(dp[i], "Laptop %d", ++val);
}
Step 6 : Print the strings
for (int i = 0; i < 10; i++)
{
printf("Row %d string : %s\n", i, dp[i]);
}
Step 7 : Free after use
for (int i = 0; i < 10; i++)
{
free(dp[i]);
}
free(dp);
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
7// Step 2 : Define an allocation function
8void get_memory(char ***tp)
9{
10 // Step 2.1 : Create Ten Single Pointers from tp[0] ... tp[9]
11 *tp = malloc( 10 * sizeof(char *) );
12
13 // Step 2.2 : Create Ten Characters in every Row
14 for (int i = 0; i < 10; i++)
15 {
16 (*tp)[i] = malloc( 10 * sizeof(char) );
17 }
18}
19
20int main(void)
21{
22 // Step 3: Define a single pointer
23 char **dp;
24 int val = 0;
25
26 // Step 4 : Pass double pointer "dp" by reference
27 get_memory(&dp);
28
29 // Step 5 : Assign strings to array of characters
30 for (int i = 0; i < 10; i++)
31 {
32 sprintf(dp[i], "Laptop %d", ++val);
33 }
34
35 // Step 6 : Print the strings
36 for (int i = 0; i < 10; i++)
37 {
38 printf("Row %d string : %s\n", i, dp[i]);
39 }
40
41 // Step 7 : Free after use
42 for (int i = 0; i < 10; i++)
43 {
44 free(dp[i]);
45 }
46
47 free(dp);
48
49 return 0;
50}
Current Module
Previous Module
Next Module
Other Modules