简体   繁体   English

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

[英]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? commandArray的类型是什么? It is a pointer to a pointer right? 它指向指针对吗?

I tried char *getcommand()...that didn't work either. 我尝试了char * getcommand()......也没用。 I am calling this function in main so I have delcared.. 我在main中调用这个函数所以我已经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. 我正在粘贴代码...建议的更改反映..遇到相同的错误,说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;
}

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;

}

The problem is commandArray is an array of pointers which is stored in getCommand()'s stack frame, so this is, technically speaking, undefined behavior. 问题是commandArray是一个指针数组,存储在getCommand()的堆栈框架中,因此从技术上讲,这是未定义的行为。 The solution is to change commandArray to a char ** and use malloc() to allocate the whole array. 解决方案是将commandArray更改为char **并使用malloc()分配整个数组。

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 ** 在main中接收指针也应该是char **类型


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

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

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 使用malloc为几个指针保留一些空间并将该值赋给指针

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

Now, cmdArray points to an area of 20 pointers. 现在, cmdArray指向20个指针的区域。 But none of those 20 pointers have a valid value. 但是这20个指针中没有一个具有有效值。 You need to allocate space for each of those 20 pointers 你需要为这20个指针中的每一个分配空间

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). cmdArray指向20个有效指针,每个指针指向一个能够保存BUFSIZ字符的存储区(或者最多为BUFSIZ - 1字符串BUFSIZ - 1长度)。

To deallocate the space you need to do the reverse: first the 20 pointers and last the pointer to pointers 要释放空间,你需要反过来:首先是20个指针,最后指向指针

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 在使用内存实现之前,不要忘记检查malloc的返回值

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. 按照其他人的建议动态分配commandArray是不够的,因为返回的已分配数组包含指向输入数组的指针,输入数组也在堆栈上,因此当函数getCommand()返回时超出范围。

To see if this is the problem change: 要查看这是否是问题的变化:

char input[BUFSIZ];

To

static char input[BUFSIZ]; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM