I am having trouble with some code in C accessing the contents of this chain of pointers:
I have these structs:
typedef struct {
unsigned int hash;
char string[10];
void *memory;
} thing;
typedef struct {
int num;
thing *thing;
} node;
typedef struct {
int size;
thing* things;
node* nodes;
} t_system;
Ok. Now I initialize everything like this:
thing* things = NULL;
things = calloc(10, sizeof(thing));
node* nodes = NULL;
nodes = calloc(10, sizeof(node));
t_system* theSystem = NULL;
theSystem = calloc(1, sizeof(t_system));
theSystem->things = things;
theSystem->nodes = nodes;
And now, I want to set this:
theSystem->nodes[2].thing = &theSystem->things[1];
After that line, if I debug and set a breakpoint theSystem nodes points to 0x0
Where am I going wrong?
if (theSystem->nodes[2].thing == NULL) {
theSystem->nodes[2].thing = &theSystem->things[1]; //this is executed
}
if (theSystem->nodes[2].thing == NULL) {
//this is not executed...
}
I can do this:
theSystem->nodes[2].thing->hash = 123;
And debugging shows the correct value for hash, and thing, but not for nodes. it points to 0x0.
theSystem->nodes[2].thing = theSystem->things[1]will not even compile. Left-hand side is a pointer. Right-hand side is a struct. Please, don't make up code. Post the real code or at least something sufficiently close to real code.if (theSystem->nodes[2].thing == NULL)is false, but debugging says otherwise.theSystem->nodes[2].thing = theSystem->things[1]line, but in any case that assignment makes no sense whatsoever. You cannot assign astructto a pointer. The code is simply meaningless. What do you expect this assignment should do?