James Wilks
James Wilks

Reputation: 740

Pointers and Arrays of Structs

I understand this is a commonly asked question, but i've still yet managed to find a solution.

I have create an array of a struct path called location path location[numLevels*levelSize];

I am also using a stack of paths called start.

The problem is transferring the proper location in the array to a function, having it create a new path to put in the array, than send it back.

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;
}

The pointer square should point to the next location of the array sent to it by the function call I try to be:

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

It has an issue with sending the specific array section i want pointed to indexed by a variable elsewhere in the code currentLocation. If I just write location it works but I'm pretty sure it won't point to the next available space in the array everytime its called even if i increment currentLocations.

Any way to explain this so I can understand the error?

Upvotes: 0

Views: 73

Answers (1)

john
john

Reputation: 87997

Frantly I don't understand the question, but I can see that this is wrong

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

you mean

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

You are passing an address to moveLocationStack, so you should use &location[currentLocation] not location[currentLocation].

Also it seems like this is wrong

    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;

surely you mean this

    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;

or maybe (assuming square is a path*) this

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

You can copy a struct in one go, you don't have to copy each item in a struct separately.

Upvotes: 1

Related Questions