C++ 从函数(c+;+;)返回字符指针数组

C++ 从函数(c+;+;)返回字符指针数组,c++,arrays,pointers,char,C++,Arrays,Pointers,Char,我试图返回char数组的数组。虽然我能够成功地创建数组,但显然返回的数组不正确 是我的语法错误还是我犯了一些我忽略的错误 下面是最相关的行,下面是完整的功能 // prototype char * testFunc(); // Function char * testFunc() { char* ptrArray[2]; return(*ptrArray); } // Assignment in main() int main {

我试图返回
char
数组的数组。虽然我能够成功地创建数组,但显然返回的数组不正确

是我的语法错误还是我犯了一些我忽略的错误

下面是最相关的行,下面是完整的功能

// prototype
  char * testFunc();

// Function
    char * testFunc() {
      char* ptrArray[2];   
      return(*ptrArray);
    }
// Assignment in main()
    int main {
      char * res = testFunc();
    }
下面是完整代码的简化版本

#include <iostream>
using std::cout;

// prototype
char * testFunc();

int main() {

    short i, j;
    char * res = testFunc();

        for (i=0; i < 2; i++)
           cout <<"This is  res[" << i << "] : " << res[i] <<"\n";


    return(0);
}

char * testFunc() {

    char word1[] = "one";
    char word2[] = "two";

    // create an array of char*
    char* ptrArray[2];

    ptrArray[0] = word1;
    ptrArray[1] = word2;

    for (int i=0; i<2; i++)
        cout <<"This is ptrArray[" << i << "] : " << ptrArray[i] <<"\n";

    return(*ptrArray);
}
#包括
使用std::cout;
//原型
char*testFunc();
int main(){
短i,j;
char*res=testFunc();
对于(i=0;i<2;i++)

cout从函数返回自动存储中分配的对象(也称为“堆栈对象”)是未定义的行为。当需要返回C中的数组时,有三个选项:

  • 返回在静态存储区域中分配的对象
  • 返回在动态存储区域中分配的对象,或
  • 获取缓冲区和最大大小,并返回数组的实际大小
  • 第一个选项很少适用,因为它使函数不可重入。第三个选项很普遍,但有局限性:当您必须返回超过缓冲区容量的数据时,需要多次调用API

    这就给我们留下了第二个选项:使用
    new
    分配要返回的内存,将数据复制到内存中,然后将结果返回给调用者。现在调用者负责释放动态内存:

    // You need two asterisks: a string needs one asterisk, you return
    // a 1-D array of strings, so you need another level of indirection.
    char ** testFunc() {
        char word1[] = "one"; // Automatic memory - needs a copy
        char word2[] = "two"; // Automatic memory - needs a copy
    
        // create an array of char*
        char** ptrArray = new char*[2];
    
        ptrArray[0] = new char[strlen(word1)+1]; // Plus one for the null terminator
        strcpy(ptrArray[0], word1);
        ptrArray[1] = new char[strlen(word2)+1]; // Plus one for the null terminator
        strcpy(ptrArray[1], word2);
        for (int i=0; i<2; i++)
            cout <<"This is ptrArray[" << i << "] : " << ptrArray[i] <<"\n";
    
        return ptrArray;
    }
    
    //需要两个星号:字符串需要一个星号,返回
    //字符串的一维数组,因此需要另一个间接级别。
    char**testFunc(){
    char word1[]=“一”;//自动内存-需要一个副本
    char word2[]=“两个”;//自动内存-需要一个副本
    //创建一个字符数组*
    字符**ptrArray=新字符*[2];
    ptrArray[0]=新字符[strlen(word1)+1];//加上一个空终止符
    strcpy(ptrArray[0],word1);
    ptrArray[1]=新字符[strlen(word2)+1];//加上一个空终止符
    strcpy(ptrArray[1],word2);
    
    for(int i=0;i从函数返回自动存储器中分配的对象(也称为“堆栈对象”)是未定义的行为。当需要返回C中的数组时,有三个选项:

  • 返回在静态存储区域中分配的对象
  • 返回在动态存储区域中分配的对象,或
  • 获取缓冲区和最大大小,并返回数组的实际大小
  • 第一个选项很少适用,因为它使函数不可重入。第三个选项很普遍,但有局限性:当您必须返回超过缓冲区容量的数据时,需要多次调用API

    这就给我们留下了第二个选项:使用
    new
    分配要返回的内存,将数据复制到内存中,然后将结果返回给调用者。现在调用者负责释放动态内存:

    // You need two asterisks: a string needs one asterisk, you return
    // a 1-D array of strings, so you need another level of indirection.
    char ** testFunc() {
        char word1[] = "one"; // Automatic memory - needs a copy
        char word2[] = "two"; // Automatic memory - needs a copy
    
        // create an array of char*
        char** ptrArray = new char*[2];
    
        ptrArray[0] = new char[strlen(word1)+1]; // Plus one for the null terminator
        strcpy(ptrArray[0], word1);
        ptrArray[1] = new char[strlen(word2)+1]; // Plus one for the null terminator
        strcpy(ptrArray[1], word2);
        for (int i=0; i<2; i++)
            cout <<"This is ptrArray[" << i << "] : " << ptrArray[i] <<"\n";
    
        return ptrArray;
    }
    
    //需要两个星号:字符串需要一个星号,返回
    //字符串的一维数组,因此需要另一个间接级别。
    char**testFunc(){
    char word1[]=“一”;//自动内存-需要一个副本
    char word2[]=“两个”;//自动内存-需要一个副本
    //创建一个字符数组*
    字符**ptrArray=新字符*[2];
    ptrArray[0]=新字符[strlen(word1)+1];//加上一个空终止符
    strcpy(ptrArray[0],word1);
    ptrArray[1]=新字符[strlen(word2)+1];//加上一个空终止符
    strcpy(ptrArray[1],word2);
    对于(int i=0;i单个“字符数组”大致相当于
    char*
    。要返回数组数组,您需要
    char**
    或者
    char[]*

    正如另一个答案所说,如果从函数内部返回指针,这些指针需要是“全局”内存,而不是仅在函数内部有效的局部变量函数返回后,局部变量不再有效,因为该堆栈空间将被下一个函数调用(或更早)覆盖

    [自最初发布以来,有人建议,
    const-char*
    和(大概)
    const-char**
    将优先考虑“const-correction”]

    我的C++生锈了…但是:

    const char** testFunc() {
        const char word1[] = "one";
        const char word2[] = "two";
    
        // create an array of char*
        const char** ptrArray = (const char **) malloc( 2 * sizeof(char *));
        ptrArray[0] = word1;
        ptrArray[1] = word2;
    
        for (int i=0; i<2; i++)
            cout <<"This is ptrArray[" << i << "] : " << ptrArray[i] <<"\n";
    
        return ptrArray;
    }
    
    const char**testFunc(){
    const char word1[]=“一”;
    const char word2[]=“两个”;
    //创建一个字符数组*
    常量字符**ptrArray=(常量字符**)malloc(2*sizeof(字符*);
    ptrArray[0]=word1;
    ptrArray[1]=word2;
    对于(int i=0;i单个“字符数组”大致相当于
    char*
    。要返回数组数组,您需要
    char**
    或者
    char[]*

    正如另一个答案所说,如果从函数内部返回指针,这些指针需要是“全局”内存,而不是仅在函数内部有效的局部变量函数返回后,局部变量不再有效,因为该堆栈空间将被下一个函数调用(或更早)覆盖

    [自最初发布以来,有人建议,
    const-char*
    和(大概)
    const-char**
    将优先考虑“const-correction”]

    我的C++生锈了…但是:

    const char** testFunc() {
        const char word1[] = "one";
        const char word2[] = "two";
    
        // create an array of char*
        const char** ptrArray = (const char **) malloc( 2 * sizeof(char *));
        ptrArray[0] = word1;
        ptrArray[1] = word2;
    
        for (int i=0; i<2; i++)
            cout <<"This is ptrArray[" << i << "] : " << ptrArray[i] <<"\n";
    
        return ptrArray;
    }
    
    const char**testFunc(){
    const char word1[]=“一”;
    const char word2[]=“两个”;
    //创建一个字符数组*
    常量字符**ptrArray=(常量字符**)malloc(2*sizeof(字符*);
    ptrArray[0]=word1;
    ptrArray[1]=word2;
    
    对于(int i=0;i非常有意义,谢谢!关于
    realloc
    的正确用法有什么提示吗?我对
    char*retArray=(char*)realloc(ptraray,sizeof(ptraray))没有任何运气;
    这个答案忽略了函数返回签名不正确这一事实——它只是一个字符指针,但需要是一个字符指针数组!他关于内存范围的提示很有帮助,t
    int main() {
        short i;
    
        // get the array -- will now be our responsibility to free
        const char** ptrArray = testFunc();
    
        for (i=0; i < 2; i++) {
            // read single pointer (char*),  from our array of pointers (char**)
            const char* word = ptrArray[i];
            cout <<"This is  res[" << i << "] : " << word <<"\n";
        }
    
        // free it.
        free( ptrArray);
    
        return(0);
    }