At the moment I have the following code:
typedef struct _hexagon {
int *vertice[6];
int *path[6];
int resourceType;
} hexagon;
typedef struct _game {
hexagon hexagons[5][5];
} Game;
and in the main I have:
Game g;
// This is the line that fails
g.hexagons[0][0].vertice[0] = 0;
This compiles fine but gives a segmentation fault. I have tried many variations, such as
g.hexagons[0][0].*vertice[0] = 0;
which doesn’t compile. How do I access a pointer’s memory from within a struct?
As
verticeis aarray-of-pointes-to-integers, to accessvertice[0], you need to do*g.hexagons[0][0].vertice[0]Sample program:
Output:
Hope it helps!
UPDATE: as pointed out by Luchian Grigore
The reason for the segmentation fault is explained by the following small program. In short, you are de-referencing a NULL pointer.
Now you can co-relate
int *ip[]with yourg.hexagons[0][0].vertice[0]