- #1
gruba
- 206
- 1
I have an array of n strings (with blank spaces). Data type of an array is char***.
How to allocate memory for char*** data type?
How to allocate memory for char*** data type?
Last edited by a moderator:
/* allocate n pointers to string */
char **array_of_pointers = (char **) malloc(n * sizeof(char *));
/* set pointer to array of pointers */
char *** pointer_to_array_of_pointers = &array_of_pointers;
One string would be of type char *. An array of them would be of type char **. Why do you need three levels of indirection? Are you asking about a function parameter that is call by reference?gruba said:I have an array of n strings (with blank spaces). Data type of an array is char***.
How to allocate memory for char*** data type?
In the second line above, best practice is to cast the pointer returned from malloc() to the appropriate type. The return type from malloc() is void *.rcgldr said:I'm not sure why you need char ***, but here is example code:
Code:/* allocate n pointers to string */ char **array_of_pointers = malloc(n * sizeof(char *)); /* set pointer to array of pointers */ char *** = &array_of_pointers;
Maybe he's trying to set up a matrix-like structure. One of your char**'s would represent a row. An array of these (char***) would be a collection of rows. Or you could do it column-wise instead of row-wise.Mark44 said:One string would be of type char *. An array of them would be of type char **. Why do you need three levels of indirection?
Missed an edit, fixed now. Will delete this post later. Some people may complain about needless casting of malloc(), but I've seen instances where some compilers complains if you don't cast malloc().Mark44 said:casting malloc() ... fourth line is missing the actual variable on the left side.
So each row would be an array of pointers to strings, and the matrix would contain some number of these rows. I'll wait for the original poster to comment on this.jtbell said:Maybe he's trying to set up a matrix-like structure. One of your char**'s would represent a row. An array of these (char***) would be a collection of rows. Or you could do it column-wise instead of row-wise.
In the case of C, a void pointer can be assigned to any pointer type, while in the case of C++, a void pointer is usually cast in order to assign it to a pointer of type other than void.newjerseyrunner said:malloc is the function that you want, and yes, you need to cast it to the type you want. Most compilers will automatically do the cast for you, but it's not good practice.
I had to check, as well, and looked at my K & R, 2nd Ed., which says the same thing.newjerseyrunner said:I checked the standard, you are correct, it is not required to cast a void *.
It certainly doesn't hurt.newjerseyrunner said:It is, however, good practice
It doesn't hurt, however, if the poster makes the change to C++, then it becomes relevant.Mark44 said:I had to check, as well, and looked at my K & R, 2nd Ed., which says the same thing.
It certainly doesn't hurt.
After re-reading the original question, it's an array of n strings, but then goes on to state with blank spaces, so perhaps n pointers into a single string that point to the first letter of each word in the string. It's not clear how or why a ***char is being used here.jtbell said:Maybe he's trying to set up a matrix-like structure.
The data type char*** in C is a pointer to a pointer to a character. It is used to store a string or an array of strings in memory.
To allocate memory for char*** data type in C, you can use the malloc()
function. This function allocates a block of memory of the specified size and returns a pointer to the first byte of the allocated memory.
The syntax for allocating memory for char*** data type in C is as follows:
char*** ptr = (char***)malloc(rows * sizeof(char**));
This allocates memory for a two-dimensional array of characters with rows
number of rows.
To free the allocated memory for char*** data type in C, you can use the free()
function. This function takes in a pointer to the allocated memory and deallocates it.
Allocating and freeing memory for char*** data type in C is important because it allows you to dynamically allocate memory for strings or arrays of strings, which can vary in size. This helps to optimize memory usage and prevent memory leaks in your program.