2016-03-22 82 views
-1

我是新的C++和我在一個函數工作洗牌串返回一個字符串*從功能型數組回主

它需要一個字符串數組,打亂他們,並返回他們回來主要。

我正在返回一個指向名爲shuffled的字符串數組的指針。我遇到的問題是,當我嘗試將新的指針保存到數組中的另一個指針時,我開始獲取奇怪的值,這些值引用計算機中的文件位置或一堆數字。

我會在這裏發佈整個代碼,但真正想看的是返回類型,我如何返回它以及如何將它保存在main中。請告訴我爲什麼我的指針沒有引用在函數中創建的工作數組。這裏是代碼:

#include <cstdio> 
#include <string> 
#include <ctime> 
#include <new> 
#include <cstdlib> 
using namespace std; 

const char * getString(const char * theStrings[], unsigned int stringNum) 
{ 
    return theStrings[stringNum]; 
} 

string * shuffleStrings(string theStrings[]) 
{ 
    int sz = 0; 
    while(!theStrings[sz].empty()) 
    { 
     sz++; 
    } 
    sz--; 
    int randList[sz]; 
    for(int p = 0; p < sz; p++) 
    { 
     randList[p] = sz; 
    } 

    srand(time(0));//seed randomizer to current time in seconds 
    bool ordered = true; 
    while(ordered) 
    { 
     int countNumberInRandList = 0;//avoid having a sz-1 member list length  (weird error I was getting) 
     for(int i = 0; i < sz; i++) 
     { 
      int count = 0; 
      int randNum = rand()%(sz+1);//get random mod-based on size 
      for(int u = 0; u < sz; u++) 
      { 
       if(randList[u] != randNum) 
       { 
        count++; 
       } 
      } 
      if(count == sz) 
      { 
       randList[i] = randNum; 
       countNumberInRandList++; 
      } 
      else 
       i--; 
     } 
     //check to see if order is same 
     int count2 = 0; 
     for(int p = 0; p < sz; p++) 
     { 
      if(randList[p] == p) 
      { 
       count2++; 
      } 
     } 
     if(count2 < sz-(sz/2) && countNumberInRandList == sz) 
     { 
      ordered = false; 
     } 
    } 
    string * shuffled[sz]; 
    for(int r = 0; r < sz; r++) //getting random num, and str list pointer from passed in stringlist and setting that value at shuffled [ random ]. 
    { 
     int randVal = randList[r]; 
     string * strListPointer = &theStrings[r]; 
     shuffled[randVal] = strListPointer; 
    } 
    for(int i = 0; i < sz; i++) 
    { 
     printf("element %d is %s\n", i, shuffled[i]->c_str());//correct values in a random order. 
    } 
    return *shuffled; 
} 

int main() 
{ 
    string theSt[] = {"a", "b", "pocahontas","cashee","rawr", "okc", "mexican", "alfredo"}; 
    string * shuff = shuffleStrings(theSt);//if looped, you will get wrong values 
    return 0; 
} 
+0

getString的寫法是錯誤的,未使用和非常嚴重不符合您的代碼的其餘部分... 作爲一個文件,儘量簡化這個問題到最小化需要重現您的問題,而不是隻問我們'找到你的錯誤'。 如果你正在嘗試學習C++;考慮使用'cout'而不是'printf'和ditcth'cstdio' ......不妨使用你正在使用的語言的功能 – UpAndAdam

+0

在你定義你的返回類型之前,你需要明確地定義你的需求和接口。你是否試圖將用戶傳入的數組進行混洗,或者你是否試圖將它複製到一個新的數組中? – UpAndAdam

+1

TL; DR std :: vector' –

回答

1

字符串分配自己的內存,沒有必要給他們「長度」,就像你必須做的字符數組。有你的代碼的幾個問題 - 沒有進入細節,這裏有幾個工作/非工作的例子,希望能幫助您:

using std::string; 

// Returns a string by value 
string s1() { 
    return "hello"; // This implicitly creates a std::string 
} 

// Also returns a string by value 
string s2() { 
    string s = "how are you"; 
    return s; 
} 

// Returns a pointer to a string - the caller is responsible for deleting 
string* s3() { 
    string* s = new string; 
    *s = "this is a string"; 
    return s; 
} 

// Does not work - do not use! 
string* this_does_not_work() { 
    string s = "i am another string"; 
    // Here we are returning a pointer to a locally allocated string. 
    // The string will be destroyed when this function returns, and the 
    // pointer will point at some random memory, not a string! 
    // Do not do this! 
    return &s; 
} 

int main() { 
    string v1 = s1(); 
    // ...do things with v1... 
    string v2 = s2(); 
    // ...do things with v2... 
    string* v3 = s3(); 
    // ...do things with v3... 
    // We now own v3 and have to deallocate it! 
    delete v3; 
} 
0

還有一堆事情錯在這裏 - 不恐慌,這是大多數人在第一次圍繞C和C++中的指針和數組包裝大腦時發生的事情。但是這意味着很難對單個錯誤置之不理,並說「這就是它」。所以我會指出一些事情。

(但提前預警:你問的指針返回到main,你的代碼確實做錯事了,並且我要談一下什麼是錯的,如何做的更好一堆東西。但是實際上不是負責你看到錯誤。)

所以,在shuffleStrings你讓指針到字符串(string * shuffled[])的陣列。您要求shuffleStrings返回一個指向字符串的指針(string *)。你能看到這些不匹配嗎?

在C和C++中,您實際上不能傳遞數組並從函數返回它們。您嘗試時的行爲往往會讓新手感到困惑。你需要在某個時候瞭解它,但現在我只會說:你實際上不應該讓shuffleStrings嘗試返回一個數組

有兩種更好的方法。第一種方法不是使用數組,而是使用向量,這是一種存在於C++中但不在C中的容器類型。您可以按值傳遞數組,並且它們將根據需要進行復制。如果您製作shuffleStrings返回vector<string*>(並且在shuffleStringsmain中進行了其他必要更改,以使用矢量而不是陣列),那可以起作用。

vector<string *> shuffleStrings(...) { 
    // ... (set things up) ... 
    vector<string *> shuffled(sz); 
    // ... (fill shuffled appropriately) ... 
    return shuffled; 
} 

但是這可能是低效的,因爲你的程序然後不得不復制一堆東西。 (在這種情況下,它可能並不那麼糟糕,因爲一小部分指針並不是很大,因爲C++編譯器有時能夠弄清楚你在這種情況下做了什麼,並避免了複製;細節沒有現在很重要。)

另一種方法是使陣列不在shuffleStrings中,但在main;將指針傳遞給該數組(或其第一個元素,結果相當於)shuffleStrings;並使shuffleStrings然後修改數組的內容。

void shuffleStrings(string * shuffled[], ...) { 
    // ... (set things up) ... 
    // ... (fill shuffled appropriately) ... 
} 

int main(...) { 
    // ... 
    string * shuffled[sz]; 
    shuffleStrings(shuffled, theSt); 
    // output strings (main is probably a neater place for this 
    // than shuffleStrings) 
} 

說了這麼多,這是造成你的症狀的問題在其他地方,裏面shuffleStrings - 畢竟,main在代碼中從未真正使用它從shuffleStrings回來的指針。

那麼究竟是什麼錯誤?我還沒有弄清楚你的洗牌代碼究竟做什麼,但那是我敢打賭問題所在。你正在製作這個指向字符串的指針數組,然後你在填充的一些的元素 - 對應於randList中的數字。但是如果randList中的數字沒有覆蓋shuffled中的全部有效索引,那麼您將會保留其中一些指針未初始化的位置,並且它們可能指向任何地方,然後詢問他們的c_str可能會給您所有的廢話。我期望這是問題所在。

0

你的問題與你所說的任何東西無關。因爲你是初學者,我建議不要假設你的代碼是正確的。相反,我會建議刪除不相信是有問題的部分,直到你沒有任何東西離開問題。

如果你這樣做,你應該很快發現你正在寫入無效的內存。

第二部分:你似乎無法確定你要返回的類型。你正在建立一個指向數組的指針來返回還是你返回一個指針數組......你似乎間歇地在這些數組之間切換。第三部分:閱讀@ Gareth的回答,他解釋瞭如何在你的實例中很好地傳遞參數。

相關問題