簡體   English   中英

從 c++ 中的 function 返回二維字符數組並打印

[英]Return 2D char array from function in c++ and print it

這是我的代碼,我想從我的 function 中返回二維數組 [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 *[10];

您只為指針分配 memory ,而不是為要存儲實際字符串的 memory 塊分配 memory :

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
}

現場演示

在正常情況下,當程序沒有立即結束時,您必須釋放 memory:

對於分配了new的 memory :

for (int i = 0; i < 10; i++) 
{
    delete words[i];
}
delete words;

對於分配有 malloc 的malloc

for(int i = 0; i < 10; i++)
{
    free(words[i]);
}
free(words);  

這在您的情況下可能很棘手,因為您返回 2 種類型的 memory 分配取決於您作為參數傳遞的選項,我的建議是您使用相同的選項來選擇如何解除分配 memory。

PS:使用像std::vectorstd::string這樣的 C++ 容器會讓你的工作更輕松,你不需要自己處理 memory。

用於

 char **words =new char*[8];
 strcpy(words[0],"Pakistan");

是一個問題,因為您沒有為words[0]分配 memory 。

它類似於

 char* cp; /// Uninitialized pointer
 strcpy(cp,"Pakistan");

在調用strcpy之前,您必須為words[0]words[1]等分配 memory 等。

更重要的是,改變你的策略,改用std::vector<std::sting> 這使您的代碼更簡單,並消除了從應用程序代碼中分配和取消分配 memory 的負擔。

std::vector<std::string> getWords(int level)
{
     std::vector<std::string> words;
     if (level==1)
     {
         words.push_backl("Pakistan");
         // etc.

     
     return words;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM