Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jan 3, 2016 #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: Jan 3, 2016
  2. jcsd
  3. Jan 3, 2016 #2

    rcgldr

    User Avatar
    Homework Helper

    I'm not sure why you need char ***, but here is example code:

    Code (Text):

    /* 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: Jan 3, 2016
  4. Jan 3, 2016 #3

    Mark44

    Staff: Mentor

    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?
    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.
     
  5. Jan 3, 2016 #4

    jtbell

    User Avatar

    Staff: Mentor

    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.
     
  6. Jan 3, 2016 #5

    rcgldr

    User Avatar
    Homework Helper

    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().
     
  7. Jan 3, 2016 #6

    rcgldr

    User Avatar
    Homework Helper

    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.
     
  8. Jan 4, 2016 #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.
     
  9. Jan 4, 2016 #8

    rcgldr

    User Avatar
    Homework Helper

    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.
     
  10. Jan 4, 2016 #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.)
     
  11. Jan 4, 2016 #10

    Mark44

    Staff: Mentor

    I had to check, as well, and looked at my K & R, 2nd Ed., which says the same thing.
    It certainly doesn't hurt.
     
  12. Jan 4, 2016 #11
    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.
     
  13. Jan 4, 2016 #12

    rcgldr

    User Avatar
    Homework Helper

    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.
     
  14. Jan 4, 2016 #13

    Mark44

    Staff: Mentor

    Let's wait and see if he comes back to enlighten us about what he's doing...
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C: How to allocate memory for char*** data type
  1. C data types problem (Replies: 10)

Loading...