1

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.

13
  • 1
    You are making something up here. The line 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. Commented Jul 17, 2012 at 6:46
  • That is exactly why im here. if (theSystem->nodes[2].thing == NULL) is false, but debugging says otherwise. Commented Jul 17, 2012 at 6:50
  • 1
    No, no, no. Again, the code you posted will not compile. It cannot be run or debugged. The line I referred to is not compilable by any existing compiler, unless you are using something totally unknown. What compiler are you using? Commented Jul 17, 2012 at 6:52
  • As far as i know, im using gcc. Commented Jul 17, 2012 at 6:55
  • Well, I don't know what kind of major malfunction in GCC allows one to compile theSystem->nodes[2].thing = theSystem->things[1] line, but in any case that assignment makes no sense whatsoever. You cannot assign a struct to a pointer. The code is simply meaningless. What do you expect this assignment should do? Commented Jul 17, 2012 at 6:58

2 Answers 2

2

You wrote

node* nodes = NULL;
tree = calloc(10, sizeof(node));

You should have written

node* nodes = NULL;
nodes = calloc(10, sizeof(node));
Sign up to request clarification or add additional context in comments.

1 Comment

I have update the code to reflect this fix, please take a look.
1

You initialize nodes to NULL on this line:

node* nodes = NULL; 

Then you assign nodes to theSystem->nodes on this line:

theSystem->nodes = nodes; 

Since you never change the value of nodes in between, theSystem->nodes will also be NULL.

Are you sure the following line is correct:

tree = calloc(10, sizeof(node));  

Shouldn't you assign this to nodes instead?

1 Comment

Yes that was wrong, but just in this question. The actual code is correct, i have updated the main question.

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.