I have a stack using structure. I need to return a string while i pop. so i try to copy the string to a pointer using strcpy(), but when i run the program, the program stops working right at that step.
Here's the code for stack.
struct node{ // stack structure
char data[5];
struct node *link;
}*top=NULL;
Here's the code for pop function.
char* pop(){
printf("\nIn pop fun.");
if(top==NULL)
{
printf("Error!!!\nStack Underflow.");
return "error";
}
printf("\nChecked if pop is null.");
char *popvar;
printf("\nCreated new char pointer.");
strcpy(popvar,top->data);
printf("\nCopied data from top.");
struct node *tmp = top;
printf("\nCreated new node.");
top=tmp->link;
printf("\n Assigned top new value.");
free(tmp);
printf("\nFree temp");
printf("\npoped from stack.");
return popvar;
}
Anyone please help...