4

I have come across a code in C where there seemed to be a multiple assignment to a pointer(in blockquotes). How does this work? The 'prev' isnt even defined.

Heres the code:

// A complete working C program to demonstrate deletion in singly
// linked list
#include <stdio.h>
#include <stdlib.h>

// A linked list node
struct Node
{
    int data;
    struct Node *next;
};

/* Given a reference (pointer to pointer) to the head of a list
and an int, inserts a new node on the front of the list. */
void push(struct Node** head_ref, int new_data)
{
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref)    = new_node;
}

/* Given a reference (pointer to pointer) to the head of a list
and a key, deletes the first occurrence of key in linked list */
void deleteNode(struct Node **head_ref, int key)
{
    // Store head node
    struct Node* temp = *head_ref, *prev;

    // If head node itself holds the key to be deleted
    if (temp != NULL && temp->data == key)
    {
        *head_ref = temp->next;   // Changed head
        free(temp);               // free old head
        return;
    }

    // Search for the key to be deleted, keep track of the
    // previous node as we need to change 'prev->next'
    while (temp != NULL && temp->data != key)
    {
        prev = temp;
        temp = temp->next;
    }

    // If key was not present in linked list
    if (temp == NULL) return;

    // Unlink the node from linked list
    prev->next = temp->next;

    free(temp);  // Free memory
}

// This function prints contents of linked list starting from 
// the given node
void printList(struct Node *node)
{
    while (node != NULL)
    {
        printf(" %d ", node->data);
        node = node->next;
    }
}

/* Drier program to test above functions*/
int main()
{
    /* Start with the empty list */
    struct Node* head = NULL;

    push(&head, 7);
    push(&head, 1);
    push(&head, 3);
    push(&head, 2);

    puts("Created Linked List: ");
    printList(head);
    deleteNode(&head, 1);
    puts("\nLinked List after Deletion of 1: ");
    printList(head);
    return 0;
}
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Deba
  • 131
  • 5

5 Answers5

4

The definition

struct Node* temp = *head_ref, *prev;

is the same as

struct Node* temp = *head_ref;
struct Node *prev;

This is some very basic syntax, something that even a pretty bad tutorial or book should have shown.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

This is delcaring a Node temp, and initializing it to head_ref. It is declaring a second Node, named prev, and not initializing it to anything

divinas
  • 1,787
  • 12
  • 12
  • Talking picky C: This pointers aren't just declared but even *defined*. You would not be able to initialise a pointer which had "only" been declared. – alk Jul 31 '18 at 16:52
  • Yes, you are correct of course. This is a definition, not a declaration. – divinas Jul 31 '18 at 21:09
1

struct Node* temp = *head_ref, *prev You declare 2 pointers to Node one is temp and the second is prev, you assign a value to temp which is head_ref deref, perfectly legal.

Dr.Haimovitz
  • 1,568
  • 12
  • 16
1

The variable *prev isn't being assigned, it is being defined. You can have definitions like this in C:

int a, b; // two integers

struct Node *a, b; // a pointer to a struct and a struct itself

The only thing that definition is doing is assigning a value to the *temp variable.

FSB
  • 49
  • 1
  • 10
  • Talking picky C: This aren't declarations, but definitions. – alk Jul 31 '18 at 16:49
  • @alk You are completely correct, I updated my answer. For anyone confused on declarations vs definitions, see https://stackoverflow.com/a/1410632/2465379 – FSB Jul 31 '18 at 19:59
0

This is not a multiple assignment (which C does not support) - it's a declaration of two objects, one of which has an initializer.

This is way more detail than you want, but I don't want to work on what I'm supposed to be working on and need a distraction.

The top-level syntax for a declaration of an object or function in C is as follows:

declaration:
    declaration-specifiers init-declarator-listopt ;

declaration-specifiers includes things like type specifiers (int, float, double, struct xxx, etc.), type qualifiers (const, volatile), and storage class specifiers (static, register, auto, typedef)

init-declarator-listopt is an optional list of declarators, each of which may have an optional initializer. A declarator introduces the name of the thing being declared, along with its array-ness, pointer-ness, and/or function-ness.

So when we look at the declaration:

struct Node* temp = *head_ref, *prev;

struct Node is part of the declaration-specifiers, while *temp = *head_ref and *prev make up the init-declarator-list (In a declaration, the * operator always binds to the declarator, not the type specifier - the above is parsed as struct Node (*temp) = (*head_ref), (*prev)).

John Bode
  • 119,563
  • 19
  • 122
  • 198