Below code gives me an error Segmentation fault (core dumped) but if I uncomment return ptr and comment return &newStack it will work fine. Why this make difference, if we consider pass by reference in function then it can take function(&variable), here we don't need to pass a pointer. I am a little confused about this concept.
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int top;
char arr[10];
}Stack;
Stack * create_stack(){
Stack newStack,*ptr;
newStack.top = -1;
ptr = &newStack;
//return ptr;
return &newStack;
}
int main(){
Stack * S1;
S1 = create_stack();
printf("%d\n",S1->top);
return 0;
}