2013-04-15 94 views
-1

所以這裏是我正在爲類工作的程序中的一個函數。在這個函數中,我想組合一個8字符的3,4,5,6,7和8個字母的單詞,這些單詞用checkDictionary函數驗證。一切工作正常,並且漂亮,但是當我想將這些創建的單詞存儲到字符的二維數組(基本上是一個一維字符串數組)時,我得到了分段錯誤錯誤。顯然導致這些問題的行是如下行:二維數組的簡單問題

strcpy(arrWords [count],letters8); strcpy(arrWords [count],letters7); ...

^直到letters3^

我只是想知道究竟是怎麼了存儲與代碼行如二維數組。此外,如果你可以給我任何關於如何準確地將這些創建的單詞存儲到二維數組(arrWords)中的建議,將不勝感激。非常感謝您的參與! :d

雜:「」

  • 這個節目基本上拼字

  • 該增量I & j中的迴路用於通過填充有10×10的2D陣列板遞增並由用戶放置單詞。

-the詞典有40438個實際的話

void computerMove(char dictionary[][45], char arrBoard[11][11], char computer[9]) 
{ 
    char let1, let2, let3, let4, let5, let6, let7, let8; 


//  char letters3[4]; 
//  char letters4[5]; 
//  char letters5[6]; 
//  char letters6[7]; 
//  char letters7[8]; 
//  char letters8[9]; 
    int count = 0; 


    char arrWords[40438][10]; 






    for (int i=0; i<10; i++) 
    { 
     for (int j=0; j<10; j++) 
     { 
     // if (arrBoard[i][j]!='*') 
      let1=arrBoard[i][j]; 

       for (int a=0; a<8; a++) 
       { 
        let2=computer[a]; 

         for (int b=0; b<8; b++) 
         { 
          let3=computer[b]; 
          char letters3[4]={let1,let2,let3}; 
          cout<<letters3<<endl; 
          if (searchDictionary(dictionary,letters3,40438)==true) 
          strcpy(arrWords[count],letters3); 
          count++; 


           for (int c=0; c<8; c++) 
           { 
            let4=computer[c]; 
            char letters4[5]={let1,let2,let3,let4}; 
            if (searchDictionary(dictionary,letters4,40438)==true) 
            strcpy(arrWords[count],letters4); 
            count++; 

             for (int d=0; d<8; d++) 
             { 
              let5=computer[d]; 
              char letters5[6]={let1,let2,let3,let4,let5}; 
              if (searchDictionary(dictionary,letters5,40438)==true) 
              strcpy(arrWords[count],letters5); 
              count++; 

               for (int e=0; e<8; e++) 
               { 
                let6=computer[e]; 
                char letters6[7]={let1,let2,let3,let4,let5,let6}; 
                if (searchDictionary(dictionary,letters6,40438)==true) 
                strcpy(arrWords[count],letters6); 
                count++; 

                 for (int f=0; f<8; f++) 
                 { 
                  let7=computer[f]; 
                  char letters7[8]={let1,let2,let3,let4,let5,let6,let7}; 
                  if (searchDictionary(dictionary,letters7,40438)==true) 
                  strcpy(arrWords[count],letters7); 
                  count++; 

                   for (int g=0; g<8; g++) 
                   { 
                    let8=computer[g]; 
                    char letters8[9]={let1,let2,let3,let4,let5,let6,let7,let8}; 
                    if  (searchDictionary(dictionary,letters8,40438)==true) 
                     strcpy(arrWords[count],letters8); 
                    count++; 
                   } 
                 } 
               } 
             } 
           } 
         } 
       } 
     } 
//   cout<<"YOURE OVER HERE PAL!"<<endl; 
// for (int z=0; z<50; z++) 
// cout<<arrWords[z]<<endl; 
} 



} 

非常感謝你的努力幫助!

+1

要求人們調試7層用於循環使用的什麼程序做的是硬沒有明確的指示。如何將程序分解爲邏輯程序,重新思考算法併發佈一個簡單的可重複代碼。當你分解它時,你很可能會發現問題 – Pradheep

+0

真的嗎?你會低估我的問題,好像它不應該回答。 Sheesh什麼卡住了。我想知道的是如何將char數組複製到2d char數組中。對不起,我試圖儘可能具體。我已經嘗試過... – Guitardeon

+0

它不是我投下的票。除了作爲一般性評論,任何有7個for循環的函數都是調試的噩夢。嘗試考慮像哈希映射等替代數據結構,這將使它更容易 – Pradheep

回答

0

strcpy用於複製C字符串,而不是數組。

此函數查找字符串結束空字符'\0'爲來源和目標輸入,以便它知道停止複製操作的位置。但在數組的情況下,它可能無法找到這個字符的長記憶序列,你會得到分割錯誤。

相反,你可以使用std::copy()這樣的:

std::copy(std::begin(letters3),std::end(letters3), arrWords[count]);