'How can I access a pointer to a struct element?
I have a struct like this:
struct Cell {
int to_cell_id;
bool landslide;
};
And I have a function to initialize the elements, which works:
void init_game(Cell* cell_ptrs[]) {
for (int i=0; i<26; i++){
for (int j=0; j<8; j++){
if(LINK_ENDS[j][0] == i){
cell_ptrs[i] ->to_cell_id = LINK_ENDS[j][1]; //use arrayname[i] -> struct to call struct array
break;
}
else{
cell_ptrs[i] ->to_cell_id = -1;
}
}
if (i==6||i==11||i==13||i==23){
cell_ptrs[i] ->landslide = true;
}
else{
cell_ptrs[i] ->landslide = false;
}
}
}
I also have another struct, with a 2d array called LINK_ENDS. First, I want to set that cell_ptrs[i+1] -> to_cell_id is equal to cell_ptrs[i] -> to_cell_id. Then I want to set that cell_ptrs[LINK_ENDS[j][0]+1] -> to_cell_id is equal to that of cell_ptrs[LINK_ENDS[j][0]] -> to_cell_id if the second column of LINK_ENDS has i found at the jth row. I have tried using two for loops to check whether they are in the columns first, and just directly assigned a variable which is equal to cell_ptrs[i] -> to_cell_id, and kept swapping the values. However, the program got crashed. I need to first make elements point to the same memory space, which I can do so, but how should I access to make the to_cell_ids equal?
I am thinking of using a variable to store the cell_ptrs[i] -> to_cell_id, but that is a garbage value??
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|