Return 来自 C++ 函数的 2D 字符数组并打印它
Return 2D char array from function in c++ and print it
这是我的代码,我想从我的函数中 return 二维数组 [10][8] 和 [10][20],但我收到错误!! (分段错误)。
请帮帮我!!我的项目需要这个。最后我想打印这个数组,但由于错误我不能这样做。
谁能帮我解决这个问题并打印出来?
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
char **getWords(int level)
{
if (level == 1)
{
char **words = new char *[8];
strcpy(words[0], "Pakistan");
strcpy(words[1], "Portugal");
strcpy(words[2], "Tanzania");
strcpy(words[3], "Thailand");
strcpy(words[4], "Zimbabwe");
strcpy(words[5], "Cameroon");
strcpy(words[6], "Colombia");
strcpy(words[7], "Ethiopia");
strcpy(words[8], "Honduras");
strcpy(words[9], "Maldives");
return words;
}
//For Hard Level
else if (level == 2)
{
char **words = (char **)malloc(sizeof(char *) * 20);
strcpy(words[0], "Tajikistan");
strcpy(words[1], "Uzbekistan");
strcpy(words[2], "Azerbaijan");
strcpy(words[3], "Bangladesh");
strcpy(words[4], "Luxembourg");
strcpy(words[5], "Madagascar");
strcpy(words[6], "Mauritania");
strcpy(words[7], "Montenegro");
strcpy(words[8], "Mozambique");
strcpy(words[9], "New Zealand");
return words;
}
}
int main()
{
getWords(1);
return 0;
}
使用
char **words =new char*[8];
strcpy(words[0],"Pakistan");
是一个问题,因为您没有为 words[0]
.
分配内存
类似于
char* cp; /// Uninitialized pointer
strcpy(cp,"Pakistan");
在调用 strcpy
.
之前,您必须为 words[0]
、words[1]
等分配内存
更重要的是,改变你的策略,改用std::vector<std::sting>
。这使您的代码更简单,并消除了从应用程序代码分配和释放内存的负担。
std::vector<std::string> getWords(int level)
{
std::vector<std::string> words;
if (level==1)
{
words.push_backl("Pakistan");
// etc.
return words;
}
通过
char **words = new char *[10];
您只是为指针分配内存,而不是为要存储实际字符串的内存块分配内存,您也需要为此分配内存:
char **words = new char *[10]; //space for 10 pointers
for(int i = 0; i < 10; i++){
words[i] = new char[10]; // space for 10 characters each line, 8 is not enough
} // you need at least 9 because of the ending nul byte
strcpy(words[0], "Pakistan");
strcpy(words[1], "Portugal");
//...
在 main 中,将它们分配给指向指针的指针并将它们打印出来,就好像它是一个字符串数组一样:
char** words = getWords(1);
for(int i = 0; i < 10; i++){
std::cout << words[i] << std::endl;
}
使用malloc
.
的第二部分也是如此
char **words = (char**)malloc(sizeof *words * 10); //space for 10 pointers
for(int i = 0; i < 10; i++){
words[i] = (char*) malloc(20); //space for 20 characters each line
}
在正常情况下,当程序没有立即结束时,您将不得不释放内存:
对于分配给 new
的内存:
for (int i = 0; i < 10; i++)
{
delete words[i];
}
delete words;
对于malloc
分配的内存:
for(int i = 0; i < 10; i++)
{
free(words[i]);
}
free(words);
这在您的情况下可能会很棘手,因为您 return 2 种类型的内存分配取决于您作为参数传递的选项,我的建议是您使用相同的选项来选择如何释放内存。
P.S.: 使用像 std::vector
和 std::string
这样的 C++ 容器会让你的工作更轻松,你不需要自己处理内存。
这是我的代码,我想从我的函数中 return 二维数组 [10][8] 和 [10][20],但我收到错误!! (分段错误)。
请帮帮我!!我的项目需要这个。最后我想打印这个数组,但由于错误我不能这样做。
谁能帮我解决这个问题并打印出来?
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
char **getWords(int level)
{
if (level == 1)
{
char **words = new char *[8];
strcpy(words[0], "Pakistan");
strcpy(words[1], "Portugal");
strcpy(words[2], "Tanzania");
strcpy(words[3], "Thailand");
strcpy(words[4], "Zimbabwe");
strcpy(words[5], "Cameroon");
strcpy(words[6], "Colombia");
strcpy(words[7], "Ethiopia");
strcpy(words[8], "Honduras");
strcpy(words[9], "Maldives");
return words;
}
//For Hard Level
else if (level == 2)
{
char **words = (char **)malloc(sizeof(char *) * 20);
strcpy(words[0], "Tajikistan");
strcpy(words[1], "Uzbekistan");
strcpy(words[2], "Azerbaijan");
strcpy(words[3], "Bangladesh");
strcpy(words[4], "Luxembourg");
strcpy(words[5], "Madagascar");
strcpy(words[6], "Mauritania");
strcpy(words[7], "Montenegro");
strcpy(words[8], "Mozambique");
strcpy(words[9], "New Zealand");
return words;
}
}
int main()
{
getWords(1);
return 0;
}
使用
char **words =new char*[8];
strcpy(words[0],"Pakistan");
是一个问题,因为您没有为 words[0]
.
类似于
char* cp; /// Uninitialized pointer
strcpy(cp,"Pakistan");
在调用 strcpy
.
words[0]
、words[1]
等分配内存
更重要的是,改变你的策略,改用std::vector<std::sting>
。这使您的代码更简单,并消除了从应用程序代码分配和释放内存的负担。
std::vector<std::string> getWords(int level)
{
std::vector<std::string> words;
if (level==1)
{
words.push_backl("Pakistan");
// etc.
return words;
}
通过
char **words = new char *[10];
您只是为指针分配内存,而不是为要存储实际字符串的内存块分配内存,您也需要为此分配内存:
char **words = new char *[10]; //space for 10 pointers
for(int i = 0; i < 10; i++){
words[i] = new char[10]; // space for 10 characters each line, 8 is not enough
} // you need at least 9 because of the ending nul byte
strcpy(words[0], "Pakistan");
strcpy(words[1], "Portugal");
//...
在 main 中,将它们分配给指向指针的指针并将它们打印出来,就好像它是一个字符串数组一样:
char** words = getWords(1);
for(int i = 0; i < 10; i++){
std::cout << words[i] << std::endl;
}
使用malloc
.
char **words = (char**)malloc(sizeof *words * 10); //space for 10 pointers
for(int i = 0; i < 10; i++){
words[i] = (char*) malloc(20); //space for 20 characters each line
}
在正常情况下,当程序没有立即结束时,您将不得不释放内存:
对于分配给 new
的内存:
for (int i = 0; i < 10; i++)
{
delete words[i];
}
delete words;
对于malloc
分配的内存:
for(int i = 0; i < 10; i++)
{
free(words[i]);
}
free(words);
这在您的情况下可能会很棘手,因为您 return 2 种类型的内存分配取决于您作为参数传递的选项,我的建议是您使用相同的选项来选择如何释放内存。
P.S.: 使用像 std::vector
和 std::string
这样的 C++ 容器会让你的工作更轻松,你不需要自己处理内存。