1

I've been reviewing the basics of singly linked list In C with materials from Stanford CS Library, where I came cross the following code:

struct node{
    int              data;
    struct node*     next;
};

struct node* BuildWithDummyNode(){
    struct node dummy;
    struct node* tail = & dummy;    // this line got me confused
    int i;
    dummy.next = NULL;
    for (i=1;i<6;i++){
        Push(&(tail->next), i);    
        tail = tail->next;
    }
    return dummy.next;
}

Probably not revenant, but the code for Push() is:

void Push(struct node** headRef, int data){
    struct node* NewNode = malloc(sizeof(struct node));
    newNode->data = data;

    newNode->next = *headRef;
    *headRef = newNode;
}

Everything runs smoothly, but I've always been under the impression that whenever you define a pointer, it must point to an already defined variable. But here the variable "dummy" is only declared and not initialized. Shouldn't that generate some kind of warning at least?

I know some variables are initialized to 0 by default, and after printing dummy.data it indeed prints 0. So is this an instance of "doable but bad practice", or am I missing something entirely?

Thank you very much!

1
  • struct node *tail points to the address of dummy (which is very much initialized). Commented Mar 17, 2017 at 22:33

2 Answers 2

2

Variable dummy has already been declared in the following statement:

struct node dummy;

which means that memory has been allocated to it. In other words, this means that it now has an address associated with it. Hence the pointer tail declared in following line:

struct node* tail = & dummy;

to store its address makes perfect sense.

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

2 Comments

Thank you. Then can you give me an example when you cannot assign a pointer variable other than mismatching types? Or is the "declaration vs initialization" problem only matters when you try to dereference the pointer?
Once a variable has been declared, it is always allocated some memory, which makes any pointer capable of storing its address. It does not matter whether the variable has been initialised or not.
1

"But here the variable "dummy" is only declared and not initialized."

The variable declaration introduces it into the scope. You are correct in deducing it's value is unspecified, but to take and use its address is well defined from the moment it comes into scope, right up until it goes out of scope.

To put more simply: your program is correct because you don't depend on the variables uninitialized value, but rather on its well defined address.

1 Comment

Thank you very much. I wish I could accept two answers. Your last sentence really cleared things up for me.

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.