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

Click For Summary
The discussion centers on how to allocate memory for a char*** data type in C, particularly in the context of managing an array of strings. Participants clarify that a char* represents a single string, while a char** is an array of strings. The necessity of using char*** is questioned, with suggestions that it might be intended for a matrix-like structure where each char** represents a row of strings. Example code is provided for allocating memory using malloc, emphasizing the importance of casting the return value to the appropriate type, although some argue that this casting is not strictly necessary in C. The conversation also touches on best practices for using pointers in C, highlighting the potential complexity and debugging challenges associated with multiple levels of indirection. Additionally, there is mention of the relevance of the typedef keyword for improving code readability and safety. The discussion concludes with a call for clarification from the original poster regarding their specific use case.
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:
Technology 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.)
 
  • #10
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.
 
  • #11
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.
 
  • #12
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.
 
  • #13
Let's wait and see if he comes back to enlighten us about what he's doing...
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
Replies
89
Views
6K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
5
Views
2K
  • · Replies 118 ·
4
Replies
118
Views
9K
  • · Replies 10 ·
Replies
10
Views
7K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
16
Views
6K
  • · Replies 6 ·
Replies
6
Views
3K