繁体   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