3

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?

4
  • Why is vertices an array of int* instead of an array of int? Commented Apr 25, 2012 at 6:58
  • Also, the code you said seg faults for you doesn't for me on Mac OS X. What system are you running on? Commented Apr 25, 2012 at 6:59
  • @DougRichardson are you running a C or a C++ program? Commented Apr 25, 2012 at 7:01
  • How do you say g.hexagons[0][0].vertice[0] = 0; line fails? Are you checking the stacktrack on gdb? You should be using a gdb in this case - correct?! Commented Apr 25, 2012 at 7:24

2 Answers 2

5

As vertice is a array-of-pointes-to-integers, to access vertice[0], you need to do *g.hexagons[0][0].vertice[0]

Sample program:

#include <stdio.h>

typedef struct _hexagon {
    int *vertice[6];
    int *path[6];
    int resourceType;
} hexagon;


typedef struct _game {
    hexagon hexagons[5][5];
} Game;

int main()
{
    int i1 = 1;
    int i2 = 2;
    int i3 = 3;
    int i4 = 4;
    int i5 = 5;
    int i6 = 6;

    Game g;
    g.hexagons[0][0].vertice[0] = &i1;
    g.hexagons[0][0].vertice[1] = &i2;
    g.hexagons[0][0].vertice[2] = &i3;
    g.hexagons[0][0].vertice[3] = &i4;
    g.hexagons[0][0].vertice[4] = &i5;
    g.hexagons[0][0].vertice[5] = &i6;

    printf("%d \n", *g.hexagons[0][0].vertice[0]);
    printf("%d \n", *g.hexagons[0][0].vertice[1]);
    printf("%d \n", *g.hexagons[0][0].vertice[2]);
    printf("%d \n", *g.hexagons[0][0].vertice[3]);
    printf("%d \n", *g.hexagons[0][0].vertice[4]);
    printf("%d \n", *g.hexagons[0][0].vertice[5]);

    return 0;   
}

Output:

$ gcc -Wall -ggdb test.c 
$ ./a.out 
1 
2 
3 
4 
5 
6 
$ 

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.

#include <stdio.h>

/*
int *ip[3];
+----+----+----+
|    |    |    |
+----+----+----+
   |    |    |
   |    |    +----- points to an int *
   |    +---------- points to an int *
   +--------------- points to an int *

ip[0] = 0;
ip[1] = 0;
ip[2] = 0;

+----+----+----+
|    |    |    |
+----+----+----+
   |    |    |
   |    |    +----- NULL
   |    +---------- NULL
   +--------------- NULL

*ip[0] -> dereferencing a NULL pointer ---> segmantation fault
*/

int main()
{
    int * ip[3];
    ip[0] = 0;
    ip[1] = 0;
    ip[2] = 0;

    if (ip[0] == NULL) {
        printf("ip[0] is NULL \n");
    }

    printf("%d \n", *ip[0]);
    return 0;
}

Now you can co-relate int *ip[] with your g.hexagons[0][0].vertice[0]

Sign up to request clarification or add additional context in comments.

4 Comments

Why does g.hexagons[0][0].vertice[0] = 0; fail?
also you have missed some parantheses. You write *g.hexagons[0][0].vertice[0] where you mean (*g.hexagons[0][0].vertice)[0] i believe.
He doesn't have that, his code is crashing at g.hexagons[0][0].vertice[0] = 0;, effectively ip[0] = 0; in your example.
thanks for the help, the explanation you added was particularly insightful.
0

I think you may have misunderstood what you have declared in _hexagon. *vertice[6] and your other array members are all arrays of pointers, so you have to treat each element like a pointer.

int x = 10;
g.hexagons[0][0].vertice[0] = &x;

Store the address of x into pointer at position 0 of your array of pointers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.