Re: String array




stathis gotsis wrote:
<alex323@xxxxxxxxx> wrote in message
news:1144768490.269465.313720@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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?

You could do something like this:

#include <stdlib.h>

#define STRING_NUMBER 10
#define STRING_LENGTH 20

char **foo(void)
{
int i;
char **temp=malloc(STRING_NUMBER *sizeof *temp);
if (temp==NULL)
/*Do something*/;
for (i=0;i<STRING_NUMBER;i++)
{
temp[i]=malloc(STRING_LENGTH);
if (temp[i]==NULL)
/*Do something*/;
}
return temp;
}

,adjusting it to your needs for variable number of strings, variable string
length and so on. In the former case you will need a mechanism to pass that
variable number of strings back to the caller. Do not forget to free all
allocated memory back in your main() program.
I have a question:
in the for-statement,you use malloc for STRING_NUMBER times,does it
mean that you must use free for STRING_NUMBER times or not?I am
confused.Hope you can help.thx.

.



Relevant Pages

  • Re: String array
    ... be stored in (char ***fragments) which is passed as an argument. ... in GDB to see the caller, the second element of the array is either ... How can I dynamically allocate an array of ... or you can pass a pointer to the variable you want it stored ...
    (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: Sets and portability (was) Re: Is ISO Pascal compatible with J&W (original) Pascal ?
    ... strings, the user can control the length by the data they process; ... >> The computer world is more complex than it's ever been (eg Unicode) ... The Pascal `Char' type can be this size (unlike C, ... > Note that ansi->wide conversion is codepage sensitive. ...
    (comp.lang.pascal.misc)
  • Re: socket communication: send & receive doesnt work right
    ... But can I actually send and receive strings? ... I did a test of sending two doubles: ... public void send_doubles(double vals, int len) throws ... char *result; ...
    (microsoft.public.win32.programmer.networks)
  • Re: Efficient strcat implementation.
    ... Programmers using the `strcat' or `wcscat' function (or the ... /* This function concatenates arbitrarily many strings. ... concat (const char *str, ...) ... if (newp == NULL) ...
    (comp.lang.c)