开发者

return an array of pointer, return type?

I have a function..

char ** getCommand()
{ 
    char *commandArray[BUFSIZ]; //declaring an array of char pointers
    // i m doing things to array

    return commandArray;
}

I am getting an error saying "conflicting types". what is the type of commandArray? It is a pointer to a pointer right?

I tried char *getcommand()...that didn't work either. I am calling this function in main so I have delcared..

main()
{
    char *commands;

    commands = getCommand();  //this didn't work 

    // So I redeclared...
    char *commands[BUFSIZ];

    commands = getCommand();  //this didn't work either.
}

Whats going on? I have never worked w/ array of pointers before... someone simplify the problem...and give me some kind of hint pls..

EDIT

Ok thanks for the suggestions...didn't work...

I am pasting the code...suggested changes reflected..getting same error, saying the getCommand has conflicting error types.

char **getCommand()
{
    int command_num;
    char input[BUFSIZ];
    char **commandArray;

    command_num = 0;
    commandArray = (char**) malloc (BUFSIZ * sizeof(char));

    fgets(input,sizeof(input),stdin);
    commandArray[command_num] = strtok(input, " "); /*breaks a string of commands into
                   开发者_运维知识库                                   tokens*/
    while (commandArray[command_num]!= NULL)
    {
        printf ("%s\n", commandArray[command_num]);
        command_num++;
        commandArray[command_num] = strtok (NULL, " ");
    }

    commandArray[command_num] = NULL;  /*very imp - adds null in the last position*/
    return commandArray;
}


You have a problem there. You're declaring the array as a local variable.
This means it will die (be realeased) in the end of the block.
if you want to return it you need to dynamically allocate it (and remember to free it later)

char** getCommand()
{ 
    char **commandArray = (char **)malloc(BUFSIZ* sizeof(char*));

    // i m doing things to array

    return commandArray;

}


With

char **cmdArray

you have a pointer to pointers. But the pointer does not have a valid value. Use malloc to reserve some space for a few pointers and assign that value to the pointer

cmdArray = malloc(20 * sizeof (char*));

Now, cmdArray points to an area of 20 pointers. But none of those 20 pointers have a valid value. You need to allocate space for each of those 20 pointers

for (k = 0; k < 20; k++) {
    cmdArray[k] = malloc(BUFSIZ);
}

Now, you're good to go :)
cmdArray points to 20 valid pointers and each of those pointers points to a memory area capable of holding BUFSIZ characters (or strings of up to BUFSIZ - 1 length).

To deallocate the space you need to do the reverse: first the 20 pointers and last the pointer to pointers

for (k = 0; k < 20; k++) {
    free(cmdArray[k]);
}
free(cmdArray);

Don't forget to check the return value of malloc before using the memory for real


The problem is commandArray is an array of pointers which is stored in getCommand()'s stack frame, so this is, technically speaking, undefined behavior. The solution is to change commandArray to a char ** and use malloc() to allocate the whole array.


Following are the 2 changes needed:

  • Should not return reference to local variable (which is stored on function stack), instead allocate memory from heap and returns its reference.

  • Receiving pointer in main should also be of type char **


char **getCommand(){
     char **command = (char **) malloc(N*BUFSIZE);
     //Do something to command array
     return command;
}

int main(){
     char **commands;
     commands = getCommand();
}


Dynamically allocating the commandArray as suggested by others is insufficient as the allocated array being returned contains pointers into the input array and the input array is also on the stack so goes out of scope when the function getCommand() returns.

To see if this is the problem change:

char input[BUFSIZ];

To

static char input[BUFSIZ]; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜