在這種方法中,我正在查看一個字符串是否出現在單詞搜索中。由於一個字符串可以出現在任何方向,我有以下方法檢查這些方向:如果語句被跳過
目前我的代碼在運行時出現分段錯誤。這是因爲我的方法中的第一個if語句根據gdb被跳過。
網格我與長相的工作是這樣的:
a r e o
o n l y
o d d a
的X,Y和在運行時(只是段錯誤之前)STR值是:
x = 0;
y = 0;
str = "add"
現在的strlen(海峽)評估爲3並且x - 3
評估爲-3。
-3 < 0
將評估真實並退出我的方法。不幸的是,該方法開始處的if語句在運行時跳過。任何人都知道發生了什麼事?
此外,我要補充的是,如果我改變我的網格的第一行:
q r e o
我得到正確的結果。
這裏是方法:
bool checkNorth (int x, int y, string str) {
//Deal with trival case and avoid array out of bounds/checking silly things
if (x-strlen(str) < 0){
return false;
}
//for each character in str
for (int i = 0; i < (strlen(str)); i++){
//If the character above in the grid is the next character in the string
if (grid[x-i][y].letter == str[i]){
//keep going
continue;
}else{
//It ain't north
return false;
}
}
//It's north
return true;
}
你爲什麼要做'strlen(str)'?使用'str.length()'(這是*** std :: string ***,對吧?) – Amit
@Amit:在C?你在想什麼? –
@Amit這是C代碼,所以我敢打賭'string'確實是'char *'的'typedef'。 –