C: How to allocate memory for char*** data type

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
12 replies · 3K views
gruba
Messages
203
Reaction score
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?
 
Last edited by a moderator:
Physics news on Phys.org
I'm not sure why you need char ***, but here is example code:

Code:
/* 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;
 
Last edited:
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?
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?
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;
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 *.
The fourth line is missing the actual variable on the left side.
 
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?
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:
casting malloc() ... fourth line is missing the actual variable on the left side.
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().
 
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.
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.
 
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.

Now, onto the big issue. Read up on how to use the typedef keyword. Even thought I'm sure in the millions of lines of code I've written in my life, I've used pointers to pointers to pointers, I would never write them that way, it's very dangerous because forgetting a reference or a dereference can be very difficult to debug.
 
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.
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.
 
I checked the standard, you are correct, it is not required to cast a void *. It is, however, good practice, and I would think most compilers would actually warn you about it (unless you use the strictest compiler settings, remember, a GNU C compiler doesn't strictly adhere to the standard without the -ansi and -pedantic parameters.)
 
newjerseyrunner said:
I checked the standard, you are correct, it is not required to cast a void *.
I had to check, as well, and looked at my K & R, 2nd Ed., which says the same thing.
newjerseyrunner said:
It is, however, good practice
It certainly doesn't hurt.
 
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.
It doesn't hurt, however, if the poster makes the change to C++, then it becomes relevant.
FYI, the official standard (a fairly recent version of it) can be found here: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf and the relevant part is 6.3.2.3 Pointers.
 
jtbell said:
Maybe he's trying to set up a matrix-like structure.
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.