簡體   English   中英

指針和結構數組

[英]Pointers and Arrays of Structs

我了解這是一個常見問題,但我仍然設法找到解決方案。

我創建了一個結構路徑數組,稱為位置路徑location[numLevels*levelSize];

我還使用了稱為start的路徑堆棧。

問題是將數組中的正確位置傳遞給函數,讓它創建要放置到數組中的新路徑,而不是將其發送回來。

bool moveLocationStack(const vector <vector<path> > &spaceStation,const int x,const int y,path   *square){
char c=spaceStation.at(y).at(x).type;
if(c!='#'){
    if(!spaceStation.at(y).at(x).visit and !spaceStation.at(y).at(x).inPath)
        square->type=spaceStation.at(y).at(x).type;
        square->type=spaceStation.at(y).at(x).visit;
        square->type=spaceStation.at(y).at(x).inPath;
        square->type=spaceStation.at(y).at(x).level;
        return 1;
    }
return 0;
}

指針正方形應該指向我嘗試成為的函數調用發送給它的數組的下一個位置:

  if(moveLocationStack(spaceStation,possibleX,possibleY,location[currentLocation])){
    }

發送要由代碼currentLocation中其他位置的變量索引的特定數組部分時,會遇到問題。 如果我只是寫位置,它可以工作,但是我敢肯定,即使我增加currentLocations,每次調用它也不會指向數組中的下一個可用空間。

有什么辦法可以解釋這個錯誤,以便我理解錯誤嗎?

我當然不明白這個問題,但是我可以看到這是錯誤的

if(moveLocationStack(spaceStation,possibleX,possibleY,location[currentLocation])){

你的意思是

if(moveLocationStack(spaceStation,possibleX,possibleY,&location[currentLocation])){

您正在將地址傳遞給moveLocationStack ,因此您應該使用&location[currentLocation]而不是location[currentLocation]

也似乎這是錯誤的

    square->type=spaceStation.at(y).at(x).type;
    square->type=spaceStation.at(y).at(x).visit;
    square->type=spaceStation.at(y).at(x).inPath;
    square->type=spaceStation.at(y).at(x).level;

當然你是這個意思

    square->type=spaceStation.at(y).at(x).type;
    square->visit=spaceStation.at(y).at(x).visit;
    square->inPath=spaceStation.at(y).at(x).inPath;
    square->level=spaceStation.at(y).at(x).level;

或(假設square是一條path*

    *square = spaceStation.at(y).at(x);

您可以一次性復制結構,而不必分別復制結構中的每個項目。

暫無
暫無

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

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