40

I'm not completely sure how to do this in C:

char* curToken = strtok(string, ";");
//curToken = "ls -l" we will say
//I need a array of strings containing "ls", "-l", and NULL for execvp()

How would I go about doing this?

2
  • 4
    If you want to split based on spaces, why have you specified ; as the delimiter? Commented Jun 25, 2012 at 23:03
  • 2
    For example: string = "ls -l; date; set +v" Commented Jun 25, 2012 at 23:05

2 Answers 2

69

Since you've already looked into strtok just continue down the same path and split your string using space (' ') as a delimiter, then use something as realloc to increase the size of the array containing the elements to be passed to execvp.

See the below example, but keep in mind that strtok will modify the string passed to it. If you don't want this to happen you are required to make a copy of the original string, using strcpy or similar function.

char    str[]= "ls -l";
char ** res  = NULL;
char *  p    = strtok(str, " ");
int n_spaces = 0, i;


/* split string and append tokens to 'res' */

while(p) {
  res = realloc(res, sizeof (char*) * ++n_spaces);

  if (res == NULL)
    exit(-1); /* memory allocation failed */

  res[n_spaces-1] = p;

  p = strtok(NULL, " ");
}

/* realloc one extra element for the last NULL */

res = realloc(res, sizeof (char*) * (n_spaces+1));
res[n_spaces] = 0;

/* print the result */

for (i = 0; i < (n_spaces+1); ++i)
  printf("res[%d] = %s\n", i, res[i]);

/* free the memory allocated */

free (res);

res[0] = ls
res[1] = -l
res[2] = (null)
Sign up to request clarification or add additional context in comments.

3 Comments

@FilipRoséen-refp Can you explain the last code block before the printing and freeing the memory, the: /* realloc one extra element for the last NULL */? I'm having difficulty understanding it
@Abdul I believe usually there is a null character at the end of each array so that the computer can differentiate between two different arrays.
if I wrap this into a function and return the res pointer, I get a warning after freeing the pointer I assigned res to, saying: warning: attempt to free a non-heap object [-Wfree-nonheap-object]. Do you have any insight of the cause?
6

Here is an example of how to use strtok borrowed from MSDN.

And the relevant bits, you need to call it multiple times. The token char* is the part you would stuff into an array (you can figure that part out).

char string[] = "A string\tof ,,tokens\nand some  more tokens";
char seps[]   = " ,\t\n";
char *token;

int main( void )
{
    printf( "Tokens:\n" );
    /* Establish string and get the first token: */
    token = strtok( string, seps );
    while( token != NULL )
    {
        /* While there are tokens in "string" */
        printf( " %s\n", token );
        /* Get next token: */
        token = strtok( NULL, seps );
    }
}

4 Comments

I understand this much, but this does not give me an array of strings from the tokens. I guess I don't understand that specific part of it.
Why token = strtok(NULL, seps);? Why the NULL?
@c650 See the linked page from MSDN, subsequent calls need the NULL.
Ah yes I did much Googling and have found out that strtok() uses a static variable to save its place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.