我有一个功能..返回一个指针数组,返回类型?
char ** getCommand()
{
char *commandArray[BUFSIZ]; //declaring an array of char pointers
// i m doing things to array
return commandArray;
}
我收到一个错误说“冲突的类型”。 commandArray的类型是什么?它是一个指向右侧的指针?
我试过char * getcommand()...那也不管用。我打电话主 所以我delcared这个功能..
main()
{
char *commands;
commands = getCommand(); //this didn't work
// So I redeclared...
char *commands[BUFSIZ];
commands = getCommand(); //this didn't work either.
}
回事请告诉我?我从来没有工作过W¯¯指针/阵列之前... 有人简化问题......给我某种暗示PLS的..
编辑建议
好,谢谢...没有工作...
我粘贴的代码...建议的变化反映..getting相同的错误,说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;
}
请张贴说明问题,整个问题只是问题的一个最小的,完整的例子。 –
您可能会尝试将您的修订发布为其中一个答案的评论 - 哪些不适用? –
哇!你的死回归! –