从函数返回char指针数组(c ++)(returning array of char pointers from a function (c++))

编程入门 行业动态 更新时间:2024-10-26 02:29:01
从函数返回char指针数组(c ++)(returning array of char pointers from a function (c++))

我正在尝试返回一个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); }

I'm trying to return an array of char arrays. While I am able to create the array succesfully, I am apparently returning it incorrectly.

Is my syntax off or am I committing some other error that I am overlooking?

Here are the most relevant lines, the full functions follow

// prototype char * testFunc(); // Function char * testFunc() { char* ptrArray[2]; return(*ptrArray); } // Assignment in main() int main { char * res = testFunc(); }

Here is a simplified version of the full code

#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); }

最满意答案

从函数返回在自动存储器中分配的对象(也称为“堆栈对象”)是未定义的行为。 当您需要在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; }

注意:您可能尚未访问标准库,因此下面的解决方案可能不适用。 但是,你应该知道上面的解决方案不是 C ++最好的解决方案:你可以重写这个机智的动态容器,并使代码更容易阅读:

vector<strig> testFunc() { vector<string> res; res.push_back("one"); res.push_back("two"); return res; }

在C ++ 11中,您可以做得更好:

vector<string> test() { return vector<string> {"one", "two"}; }

Returning objects allocated in the automatic storage (also known as "stack objects") from a function is undefined behavior. When you need to return an array in C, you have three options:

Return an object allocated in the static storage area, Return an object allocated in the dynamic storage area, or Take a buffer and max size, and return the actual size of the array.

The first option is rarely applicable, because it makes your function non-reentrant. The third option is widespread, but it has limitations: when you must return more data than fits into the buffer, the API needs to be called multiple times.

This leaves us with option number two: use new to allocate the memory that you are returning, copy the data into it, and return the result to the caller. It is now caller's responsibility to free the dynamic memory:

// 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; }

Note: you may not have reached the standard library yet, so the solution below may not apply. However, you should know that the above solution is not the best C++ can do: you can rewrite this wit dynamic containers, and make the code much easier to read:

vector<strig> testFunc() { vector<string> res; res.push_back("one"); res.push_back("two"); return res; }

In C++11 you can do even better:

vector<string> test() { return vector<string> {"one", "two"}; }

更多推荐

本文发布于:2023-07-28 19:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1308200.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   指针   函数   char   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!