Pointer initialization

Posted by SoulBeaver on Stack Overflow See other posts from Stack Overflow or by SoulBeaver
Published on 2010-06-06T17:06:05Z Indexed on 2010/06/06 17:12 UTC
Read the original article Hit count: 293

Filed under:
|
|

Sorry if this question has been asked before. On my search through SO I didn't find one that asked what I wanted to know.

Basically, when I have this:

typedef struct node
{
    int data;
    node *node;
} *head;

and do node *newItem = new node;

I am under the impression that I am declaring and reserving space, but not defining, a pointer to struct node, is that correct?

So when I do

newItem->data = 100 and newItem->next = 0

I get confused. newItem = 0would declare what exactly? Both data and next? The object as a whole?

I'm especially confused when I use typedef. Which part is the macro? I assume node because that's how I call it, but why do I need it?

Finally, what happens when I do:

node *temp;
temp = new node;

temp = head->next;
head->next = newItem;
newItem->next = temp;

I mean, head->next is a pointer pointing to object newItem, so I assume not to newItem.data or next themselves. So how can I use an uninitialized pointer that I described above safely like here? is head now not pointing to an uninitialized pointer?

© Stack Overflow or respective owner

Related posts about c++

Related posts about pointers