#include<stdio.h>
 
typedef struct node
 {
    int data;
    struct node * link;
 }n;
 
	void addatbeg(n *);
 	void display(n *);
 	n* first=NULL;
 


int main()
 {
    int ch;
    printf("%u",first);
    while(1)
    {
    	printf("\n enter 0 to terminate else any other key");
    	scanf("%d",&ch);
   		if(ch==0)
    	break;
    	addatbeg(first);
    }
 }
    
	
	void addatbeg(n* first)
    {
    	int ele;
    	n *new;
    	printf("enter the element");
    	scanf("%d",&ele);
    	new=(n*)malloc(sizeof(n));
    	new->data=ele;
    	printf("\nddd%u\n",first);
    	if(first==NULL)
    	{
    		new->link=NULL; 
    	}
    	else
    	new->link=first;
    	printf("\nnew=%u\n",new);
    	first=new;
    	printf("\n first=%u\n",first);
    	printf("%u",first);
    	printf("\n%u",new->link);
    	printf("\n before function is called first=%u",first);
    	display(first);
    }
	
void display(n* temp)
{
	printf("\n inside function,first=%u",first);
	while(1)
	{		
		printf("\ne");
		printf("\n%d->",temp->data);
		if(temp->link==NULL)
			break;
		temp=temp->link;
	}
}

i am trying to implement a linked list with a function to ADD ELEMENTS AT THE BEGINNING.

i have used a variable,first that holds the first node of the linked list.

but when i pass it to the display function,its value becomes NULL without doing anything. so what is the mistake??

WHY IS THE VALUE OF THE VARIABLE first getting modified??

Dani AI

Generated

The surprising NULL comes from two common C mistakes: name shadowing and treating pointer arguments as references. The function parameter first in your addatbeg is a local copy that hides the global first, so first = new; only changes that local copy. Later display prints the global first (still NULL), which explains what you see. 's suggestion to avoid the confusion is correct: either return the new head from the insertion function and assign it in main, or modify the head in-place by passing a pointer-to-pointer.

If you prefer in-place modification, change the function to accept n **head and update *head. For example:

void add_at_begin(n **head) {
    n *new = malloc(sizeof *new);
    if (new == NULL) return;
    int val;
    scanf("%d", &val);
    new->data = val;
    new->link = *head;
    *head = new;
}

/* call with */
add_at_begin(&first);

Practical debugging and safety tips: always include <stdlib.h> so malloc is properly declared; check malloc for NULL; prefer sizeof *p over sizeof(n); do not rely on printf("%u", ptr) — use printf("%p\n", (void *)ptr) to print addresses. Compile with warnings (gcc -std=c11 -Wall -Wextra -g) and use sanitizers or valgrind (-fsanitize=address,undefined or valgrind ./a.out) to catch undefined behavior or memory errors early.

Finally, either approach (return new head or use n **) is fine; pick the one that fits your code style. Also avoid reusing the same identifier for globals and parameters — that name collision is the root cause here. Refer to for the return-the-head pattern if you prefer that style.

WHY IS THE VALUE OF THE VARIABLE first getting modified??

You have too many variables named first , and it's confusing you. In particular, you have a global first and a local first which hides the global one. Compare and contrast with my version of your program:

#include <stdio.h>
#include <stdlib.h> /* This is needed for malloc() */

typedef struct node {
    int data;
    struct node *link;
} node; /* n is too short to be meaningful */

node *addfront(node *list, int value);
void display(node *list);

int main(void)
{
    node *list = NULL;
    int value;

    while (1) {
        printf("Enter an integer: ");
        fflush(stdout);

        /* An invalid integer or end-of-file will terminate the loop */
        if (scanf("%d", &value) != 1)
            break;
        
        /* Note how we're updating list */
        list = addfront(list, value);
        
        /* addfront() shouldn't be displaying the list */
        display(list);
        printf("\n");
    }
}

node *addfront(node *list, int value)
{
    /* This is the correct pattern for malloc() in C */
    node *new = malloc(sizeof *new);
    
    /* Always make sure malloc() succeeded */
    if (new != NULL) {
        new->data = value;
        
        /* This can be unconditional if list is initialized to NULL */
        new->link = list;
        
        list = new;
    }
    
    return list;
}

void display(node *list)
{
    while (list != NULL) {
        printf("%d", list->data);
        
        if(list->link==NULL)
            break;
        
        printf("->");
        list = list->link;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.