简体   繁体   English

C 中的分段错误,用于使用带有指针的 arrays 实现堆栈

[英]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.我有这个代码用于堆栈实现,使用 arrays 和指针,这些指针执行许多操作,如推送、窥视、弹出、销毁。 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:我已经通过全局声明 Stack 和 Stacktop 来完成此操作,但现在我想为这个实现使用指针,所以这是我的代码:

#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.当我在 repl.it 上执行此操作时,我能够提供第一个输入,即选项编号,但随后它给了我一个分段错误,但是当我在代码块上执行此操作时,我得到的进程返回了一些数字和一个十六进制代码。

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.因为p已经是一个指针。

But if you want to use thing you need to write:但是如果你想使用你需要写thing

Foo(&thing);

because &thing points to thing just as p points to thing .因为&thing指向thing就像p指向thing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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