0

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 i.e. 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 ?

4
  • 2
    Compile with warnings enabled and treat them as errors. Warnings such as passing argument 2 of 'Empty' from incompatible pointer type are almost alway errors. Commented Aug 17, 2020 at 8:36
  • as said by previous remarks you have plenty of warning being error, if you use gcc compile with options -Wall -Werror else do the same for the compiler you use. You must have no warning when you compile even requiring the max warning level Commented Aug 17, 2020 at 8:41
  • 2
    Basically remove most of the & characters. There may be more problems though, I didn't check. Commented Aug 17, 2020 at 8:42
  • 2
    As @Jabberwocky said. there are some big code errors with pointers. pointers as arguments should be passed as is and not with & in front otherwise you are passing the pointer of the pointer... Commented Aug 17, 2020 at 8:53

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.