#Splitting a word into an arrays of strings

14 messages · Page 1 of 1 (latest)

spring boltBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

half epoch
#

I think the best option is to have an output parameter

#

Basically take an array of char* as arguments then write the data to that instead of creating a local array

#

Then it is the caller's responsibility to either create an array that can hold all the pointers and pass that to your function

lime linden
#

Something like this?

#
char* getArguments(char* argsList, char* args) {
    int i = 0;

    while (strtok(argsList, " ")) {
        i = i + 1;
        args[i] = strtok(NULL, " ");
    }

    return args;
}
#

That does have the warning: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
args[i] = strtok(NULL, " ");

half epoch
#

char **args

#

Also you can return void, or some other meaningful value, like how many tokens parsed

lime linden
#

Got it!

And to call it you'd do something like this?

    char *string = "this is a string";
    char* args[10];

    getArguments(string, *args);
obtuse atlas
#

I'd say args without the *

lime linden
#

!closed

#

!close

spring boltBOT
#

Thank you and let us know if you have any more questions!