返回一个指针数组,返回类型?

Naz*_*Naz 2 c arrays pointers char

我有一个功能..

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

    return commandArray;
}
Run Code Online (Sandbox Code Playgroud)

我收到一个错误说"冲突的类型".commandArray的类型是什么?它指向指针对吗?

我尝试了char*getcommand()......也没用.我在main中调用这个函数所以我已经delcared ..

main()
{
    char *commands;

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

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

    commands = getCommand();  //this didn't work either.
}
Run Code Online (Sandbox Code Playgroud)

这是怎么回事?我之前从未使用过指针数组......有人简化了问题...并给我一些提示......

编辑

好的,谢谢你的建议......没有用......

我正在粘贴代码...建议的更改反映..遇到相同的错误,说getCommand有冲突的错误类型.

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;
}
Run Code Online (Sandbox Code Playgroud)

Yoc*_*mer 7

你有问题.您将数组声明为局部变量.
这意味着它将在块的末尾死亡(被重新发布).
如果你想要返回它,你需要动态分配它(并记得以后释放它)

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

    // i m doing things to array

    return commandArray;

}
Run Code Online (Sandbox Code Playgroud)