0

Below is the program written in C.

#include<stdio.h>
#include<stdlib.h>
struct stack{
    int data;
    struct stack *next;
};
void push(struct stack *t,int d){
    struct stack *nptr;
    nptr=(struct stack*)malloc(sizeof(struct stack));
    nptr->data=d;
    nptr->next=t;
    t=nptr;
}
void display(struct stack *h){
    while(h!=NULL){
        printf("%d\n",h->data);
        h=h->next;
    }
}
void pop(struct stack *t){
    struct stack *temp=t;
    t=t->next;
    free(temp);
}

int main(){
    struct stack *top=NULL;
    push(top,5);
    push(top,6);
    push(top,7);
    display(top);
    pop(top);
    display(top);
    return 0;
}

When the function is passed with pointer, as shown in the program the output is:"shell returned-1073741819".

When function is passed without parameters by declaring top as global variable the code runs fine. I would like to know what mistake I have done in the program. Please help me out.

user4581301
  • 33,082
  • 7
  • 33
  • 54
sushi
  • 65
  • 6
  • Recommendation: When you see a number like -1073741819, convert it to hex (C0000005). If it has a recognizable pattern or otherwise doesn't look random (but that's the thing about random) look it up. The program is probably trying to tell you something. – user4581301 Feb 22 '22 at 17:01
  • 2
    You can pass an object by reference with a pointer, but the pointer itself is still a plain old variable being passed by value. – user4581301 Feb 22 '22 at 17:02

0 Answers0