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

In summary, an array of n strings would be of type char **. A function to allocate memory for char *** data type would look like this: char **array_of_pointers = (char **) malloc(n * sizeof(char *)); char ***pointer_to_array_of_pointers = &array_of_pointers;
  • #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?
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
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:
  • #3
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.
 
  • #4
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.
 
  • #5
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().
 
  • #6
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.
 
  • #7
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.
 
  • #8
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.
 
  • #9
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...
 

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

What is the data type char*** in C?

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.

How do I allocate memory for char*** data type in C?

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.

What is the syntax for allocating memory for char*** data type in C?

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.

How do I free the allocated memory for char*** data type in C?

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.

What is the importance of allocating and freeing memory for char*** data type in C?

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.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
5
Views
921
  • Programming and Computer Science
4
Replies
118
Views
6K
  • Programming and Computer Science
Replies
1
Views
382
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
3
Replies
73
Views
5K
Back
Top