简体   繁体   中英

Segmentation fault in C for Stack implementation using arrays with pointers

I have this code for stack implementation using arrays with pointers which performs lot of operations like push, peep, pop, destroy. I already done this by declaring globally Stack and Stacktop that time it worked but now I am want to use pointer for this implementation, so here is my code:

#include <stdio.h>
 
int Full(int Stack[], int *StackTop, int *StackSize){
   if (*StackTop == *StackSize-1){
       return 1 ;
   }
   else{
       return 0;
   }
}
 
int Empty(int Stack[], int *StackTop){
   if (*StackTop == -1){
       return 1 ;
   }
   else{
       return 0;
   }
}
 
void PushStack(int ele, int Stack[], int *StackTop, int *StackSize){
   if (!Full(Stack, &StackTop, &StackSize)){
       Stack[++(*StackTop)]= ele ;
   }
   else{
       printf("Error: Stack is full");
   }
}
 
int PopStack(int Stack[], int *StackTop){
   if(!Empty(Stack, &StackTop)){
       printf("%d popped !",Stack[*StackTop--]);
   }
   else{
       printf("Error : Stack is Empty");
   }
}
 
void PeepStack(int Stack[], int *StackTop){
   if(!Empty(Stack, &StackTop)){
       printf("%d", Stack[*StackTop])  ;
   }
   else{
       printf("Error : Stack is Empty");
   }
}
 
int DestroyStack(int Stack[], int *StackTop){
   printf("Destroying Stack\n");
   if (!Empty(Stack, &StackTop)){
       while(!Empty(Stack, &StackTop)){
           PopStack(Stack, &StackTop);
           printf("\n");
       }
   }
   else{
       printf("Stack is already Empty");
   }
}
 
int DisplayStack(int Stack[], int *StackTop){
   int i ;
   if(Empty(Stack, &StackTop)){
       printf("Stack is Empty");
   }
   else{
       printf("Displaying Stack ....\n");
       for(i=StackTop; i>=0; --i){
           printf("| %d |\n",Stack[i]);
       }
   }
}
 
 
int main(void) {
  int StackSize = 5 ;
  int Stack[5] ;
  int StackTop = -1 ;
  int *Top = &StackTop ;
  int *Size = &StackSize ;
   while(1){
       int option, ele ;
       printf("\n Options : \n");
       printf("1. Push \n");
       printf("2. Pop \n");
       printf("3. Peep \n");
       printf("4. Destroy \n");
       printf("5. Display \n");
       printf("6. Exit \n");
       printf("Enter Option number : ");
       scanf("%d", &option);
       switch(option){
           case 1 :
           printf("Enter element you want to push : ");
           scanf("%d", &ele);
           PushStack(ele,&Stack,&Top, &Size);
           break;
          
           case 2 :
           PopStack(Stack, &Top);
           break;
 
           case 3 :
           PeepStack(Stack, &Top);
           break;
 
           case 4 :
           DestroyStack(Stack, &Top);
           printf("Stack Destroyed succesfully !");
           break ;
 
           case 5 :
           DisplayStack(Stack, &Top);
           break;
 
           case 6 :
           break ;
 
           default:
           printf("Invalid option");
 
       }
       printf("\n");
       if(option==6){
           break;
       }
   }
   return 0;
}

When I execute this on repl.it, I am able to give first input ie option number but then it gives me a segmentation fault but when I execute this on codeblocks, I get process returned with some numbers and one hexadecimal code.

So what's wrong in this code? because of which line I am getting this error?

You're using the & operator on a pointer, which makes is a pointer to a pointer.

Basically the pattern you have is this:

void Foo(int *bar)
{
  *bar = 123;
}

int main()
{
  int thing;
  int *p = &thing;   // p points to thing

  Foo(&p);
  printf("%d", &p);  // your expected output is: 123
}

But instead of:

Foo(&p);

you want:

Foo(p);

Because p is already a pointer.

But if you want to use thing you need to write:

Foo(&thing);

because &thing points to thing just as p points to thing .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM