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

Click For Summary

Discussion Overview

The discussion revolves around the allocation of memory for a data type of char*** in C, particularly in the context of handling an array of strings. Participants explore the necessity of using three levels of indirection and the implications of such a choice.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants question the need for a char*** type, suggesting that a char** may suffice for an array of strings.
  • Example code is provided for allocating memory for char***, but some participants note issues with the code, such as missing variable assignments.
  • There is a suggestion that the original poster might be trying to create a matrix-like structure, where char** represents rows of strings.
  • Participants discuss the practice of casting the return value of malloc(), with some arguing it is unnecessary while others advocate for it as good practice.
  • Some participants reference the C standard regarding void pointers and the casting of malloc(), noting that while casting is not required, it may be beneficial in certain contexts.
  • There is speculation about the original poster's intent, with suggestions that the char*** could be pointing to multiple strings or even parts of a single string.

Areas of Agreement / Disagreement

Participants express differing views on the necessity and implications of using char***, with no consensus reached on the best approach or the original poster's intent.

Contextual Notes

Some participants highlight the potential dangers of using multiple levels of pointers, noting that it can complicate debugging. There are also discussions about the standards and practices surrounding memory allocation in C.

Who May Find This Useful

This discussion may be useful for programmers working with C who are dealing with complex data structures, particularly those involving dynamic memory allocation and pointer management.

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
4K
Replies
89
Views
7K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
5
Views
2K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 10 ·
Replies
10
Views
7K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
16
Views
6K