I'm having issues with some code that I'm writing for a class homework, which is to write a simple shell for linux. I was wandering if anyone knew a way to convert a char* to a char** in a better way that what I am using.
Currently I'm accepting in a char*, making a copy of it so that I don't modify the passed char*, and using strtok to try to split the contained strings into an array of char*. I'm kind of fuzzy on the last portion, is a datatype of char* str[] the equivalent of a char**?
The main reason I'm doing this is that I need to feed that char* into execvpe and it requires a value of char**. I've included my code below.
Thank you,

createArgv(char *passedString){
    int counter;
    counter = 0;

    char *tempString, *cpPassedString;
    cpPassedString = malloc(sizeof(INPUT_BUFFER));
    *cpPassedString = *passedString;

    tempString = strtok(cpPassedString," \n");
    char *tempArray[INPUT_BUFFER];

    while(tempString != NULL){
        tempArray[counter] = tempString;
        tempString = strtok(NULL, " \n");

    return tempArray;

Edit: Why in the world is my text different colors?

Edited 3 Years Ago by laosland_1: format of post is weird.

I changed *cpPassedString = *passedString; to strcpy(cpPassedString, passedString); and changed the return type to a const char\**.

line 14: It looks like INPUT_BUFFER is a macro defined to be some number. sizeof(INPUT_BUFFER) in that case will be 4 because sizeof(<any integer>) is 4 on a 32-bit compiler (that may not be true for all compilers, but it is true for all the most common ones). Just remove use of sizeof from that line

line 15 is not making a copy of anything -- use strcpy() to make a copy of the string, the assignment operator = will not do that. Line 15 should read this strcpy(cpPassedString,passedString);

line 21: You are attempting to store a temp pointer into an array which will get destroyed when the array is returned on line 26. All the pointers that the array contains will become immediately invalid because tempArray does not own the data. On line 21 you need to allocate memory for and copy the strings using strcpy instead of just assigning the pointers. One easy quick way to do that is call strdup() like this: tempArray[counter] = strdup(tempString);

line 26: you need to free() the memory allocated on line 14.

This article has been dead for over six months. Start a new discussion instead.