0

can anyone help? why '&' is not required while calling a function in this program? but is thought that '&' is required in call by reference.

#include <stdio.h>
#include <stdlib.h>

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

void traversal(struct node *ptr)
{
    while(ptr!=NULL)
    {
        printf("%d\n", ptr->data);
        ptr = ptr->next;
    }
}

int main()
{
    struct node *head;
    struct node *second;
    struct node *third;

    head = (struct node*) malloc(sizeof(struct node));
    second = (struct node*) malloc(sizeof(struct node));
    third = (struct node*) malloc(sizeof(struct node));

    head->data = 7;
    head->next = second;

    second->data = 5;
    second->next = third;

    third->data = 12;
    third->next = NULL;

    traversal(head);

    return 0;
}

can anyone help? why '&' is not required while calling a function in this program? but is thought that '&' is required in call by reference.

3
  • Because head is already a pointer, to the struct that was allocated dynamically. Commented Apr 9, 2023 at 10:37
  • There's no "pass-by-reference" in C. You pass references (pointers) by value. Commented Apr 9, 2023 at 11:50
  • Also, casting the result of malloc is not needed in C. The pointer conversion from void* is implicit. (The cast would be needed in C++, but there you shouldn't use malloc anyway). Commented Apr 9, 2023 at 16:18

2 Answers 2

3

Don't get caught in the Cargo Cult Programming trap, where you see a pattern and apply it without understanding the semantics and why or when the pattern is appropriate.

In this case the requirement is not to blindly apply an & to all pass-by-reference calls. The requirement is merely to pass a reference. In this case a struct node*. If you have a struct node object, the address of operator (&) yields a struct node*. However in your case head is already a struct node*, so taking its address would yield a struct node** (a pointer-to-a-pointer to a struct node), and not a type matching the parameter declaration of traversal().

That is to say in this case head is already of the correct type, you do not need to take its address, it already is an address.

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

Comments

1

You have a singly linked list where nodes are linked by pointers.

The function traversal does not accept an object of the type struct node. It accepts a pointer to an object of the type struct node *.

void traversal(struct node *ptr)
{
while(ptr!=NULL)
{
printf("%d\n", ptr->data);
ptr = ptr->next;
}
}

As the original pointer used as an argument expression is not changed within the function then there is no any sense to pass it to the function by reference through a pointer to it.

Dereferencing pointers within the function as for example

ptr->data

the function has a direct access to data members of nodes pointed to by pointers.

That is is the object of the type struct node that is indeed is passed by reference to the function through a pointer to it. But the pointer itself is passed by value.

To make it clear consider the following simple demonstration program.

#include <stdio.h>

void f( int *px )
{
    printf( "x = %d\n", *px );
}

int main( void )
{
    int x = 10;

    int *px = &x;

    f( px );
}

As you can see to output the value of the variable x declared in main within the function f using the pointer px to x there is no need to pass the pointer itself by reference through a pointer to it. However the object x is passed to the function by reference indirectly through the pointer px.

Comments

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.