Re: String array



alex323@xxxxxxxxx opined:

Hey everybody. I am coding a function that takes a string as an
argument (char *msg) and breaks it up into pieces. The result will
be stored in (char ***fragments) which is passed as an argument. The
caller calls myfunction(mymsg, &my_fragment_array) where
`my_fragment_array' is a char **. My problem is that the array is
perfectly constructed inside the function, but when I switch frames
in GDB to see the caller, the second element of the array is either
null or memory garbage. How can I dynamically allocate an array of
strings from inside a function for use outside a function?

Well, you just need to pass the pointer to the allocated memory out of
the function. You can either return it as the return value of the
function, or you can pass a pointer to the variable you want it stored
in, and update its value inside the function.


--
Linux! Guerrilla UNIX Development Venimus, Vidimus, Dolavimus.
(By mah@xxxxxxxxxx, Mark A. Horton KA4YBR)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

.



Relevant Pages

  • Re: Comments ok?
    ... and the resulting undefined behavior may have caused `p' to ... Could it be a single char, an unitialised pointer, ... the caller will provide an ...
    (comp.lang.c)
  • Re: Comments ok?
    ... since the act of calling a function initializes all the ... Could it be a single char, an unitialised pointer, ... the caller will provide an ...
    (comp.lang.c)
  • Re: String array
    ... stored in (char ***fragments) which is passed as an argument. ... GDB to see the caller, the second element of the array is either null ... How can I dynamically allocate an array of strings ...
    (comp.lang.c)
  • Re: String array
    ... stored in (char ***fragments) which is passed as an argument. ... GDB to see the caller, the second element of the array is either null ... How can I dynamically allocate an array of strings ...
    (comp.lang.c)
  • Re: Problem with va_ macros and arrays of arrays
    ... > the arrays passed to a ... > specific char, somewhat similar to what the standard function ... that with an array of struct, or possibly a pointer to a dynamic array ... > As I'm still a beginner in C without a copy of the standard I ...
    (comp.lang.c)