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.
headis already a pointer, to thestructthat was allocated dynamically.mallocis not needed in C. The pointer conversion fromvoid*is implicit. (The cast would be needed in C++, but there you shouldn't usemallocanyway).