【问题标题】:C - Segmentation fault trying to put the top of a Stack into a Char arrayC - 尝试将堆栈顶部放入 Char 数组的分段错误
【发布时间】:2019-06-30 20:33:33
【问题描述】:

我的代码出现分段错误,无法解释原因。我正在编写一个程序,它采用中缀表达式并使用 stuct 堆栈转换为后缀。

这是我的主要方法和声明以及我的堆栈声明:

#define LINELN 72
#define STACKSZ 25 
#define NEWLN '\n' 
#include <stdlib.h>
#include <string.h>

typedef struct stack {
char data[STACKSZ];
int top;
} stack;

void initstk(stack *s1);
int emptystk(stack s1);
char peek(stack s);
int getline(char infix[]);
void push(char item, stack *s);
char pop(stack *s);
int preced(char x);
void processinfix(char infix[], int l, char postifx[]);
#include <stdio.h>

int main() {

char infix[LINELN];
char postfix[LINELN];

processinfix(infix,getline(infix),postfix);

 exit(0);
}

我已经测试过并且正在运行的方法是基本的堆栈操作方法:

//makes top of stack -1
void initstk(stack *s1) {

s1->top=-1;
}

//returns 1 if stack is empty, 0 otherwise
int emptystk(stack s) {


if(s.top == -1){
return one;
}
else{
    return 0;

}
}
 //peeks at top of the stack
char peek(stack s) {
return s.data[s.top];
}
//pushes value to top of stack 
void push(char item, stack *s) {
   s->top = s->top +1;
   s->data[s->top] = item;

}
//pops off the top of stack and returns it
char pop(stack *s) {

char x;
x = s -> data[s->top];
s-> top =s->top-1;
return x;
}
//gives character a precedence number according to what it is
int preced(char x) {

switch (x) {
    case '(':
    case ')':
        return 3;
    case '*':
    case '/':
    case '%':
        return 2;
    case '+':        case '-':
        return 1;
  }
    return 0;
}

我有一个 getLine() 函数,它接收用户输入的表达式,例如:A+B,并将其存储在中缀[]

int getline(char infix[]){

    char c;
    int i=0;
    printf("Type in your expression");
    while((c = getchar()) !=NEWLN && i <= LINELN && c != ' ' ){
    infix[i]=c;
    i++;
}
return i;   


}

我的下一个函数是对中缀表达式进行排序并将其放入 postfix[]

   void processinfix(char infix[], int inlen, char postfix[])
  {
    stack s1;
    initstk(&s1);
    printf("%d characters are in infix\n", inlen);
    int i =0;       
    int k=0;

            // while "i" isnt bigger then the number of elements in infix
    while( i<sizeof(infix)){

           /* if infix[i] is a letter send it to post fix right away, 
             increment inlen down one because infix is now smaller by 1 */

if (('a'<=  infix[i]&& infix[i] <= 'z') ||( 'A' <= infix[i]&& infix[i] 
     <='Z')){

        printf("%c is a letter, sending to postfix\n", infix[i]);
        postfix[k] = infix[i];
        inlen--;
        printf("infix has shrinked by 1\n");
        k++;


}
 /* else if infix[i] has a higher precedence then whats on top of stack 
  push infix[i] on to stack and increment inlen down 1 */
  else if(preced(infix[i])>preced( s1.data[s1.top])){

       push(infix[i],&s1);
       inlen--;
       printf("infix has shrinked by 1\n");
       printf("%c is top of stack\n", s1.data[s1.top]);

} 

   /* else if infix[i] is lower or equal to whats on top of stack. put 
   top of  stack into postfix[] until infix[i] is higher precedene then 
    top of stack. then put it on top of stack and decrease */

else if(preced(infix[i]) <= preced(s1.data[s1.top])){

       printf("%c is  higher or equal  then %c\n", s1.data[s1.top], 
       infix[i]);

    while (preced(infix[i])<=preced(s1.data[s1.top])){
        postfix[k] = s1.data[s1.top];
        printf("%c is put into postfix expression\n", s1.data[s1.top]);
        pop(&s1);
        k++;

 }

    push(infix[i], &s1);
    inlen--;
    printf("%c is now the top\n", s1.data[s1.top]);

 }

// if nothing is left in infix pop stack until s1.top = -1 

if(inlen==0){
     printf("nothing left in infix\n");
     while (s1.top!= -1){
     printf("sendin %c to postfix\n", s1.data[s1.top]);
     postfix[k]=s1.data[s1.top]
     pop(&s1);
     k++;
}

break;
}

i++;
}

// print postfix
int x=0;
while(x<= sizeof(postfix)){
printf("%s",postfix[x]);
x++;
}
printf("\n");
}

我得到的输出是

Type in your expressionA+B
3 characters are in infix
A is a letter, sending to postfix
infix has shrinked by 1
infix has shrinked by 1
+ is top of stack
B is a letter, sending to postfix
infix has shrinked by 1
nothing left in infix
sending + to postfix
Segmentation fault

所以在遇到问题之前一切都运行良好

if(inlen==0){
    printf("nothing left in infix\n");
    while (s1.top!= -1){
    printf("sendin %c to postfix\n", s1.data[s1.top]);
    THIS LINE -> postfix[k]=s1.data[s1.top]<-THIS LINE
    pop(&s1);
    k++;
}

后缀[k] = s1.data[s1.top];在 while 循环中较早工作,但不是在这里。关于为什么的任何想法?

【问题讨论】:

  • 请正确缩进您的代码。
  • 也许可以通过调试编译并在崩溃时查看堆栈跟踪?
  • 而且,为了让我们能够帮助您,您需要向我们展示实际代码。您在此处显示的内容无法编译,因此您不能一直在运行它。但建议是在启用警告的情况下进行编译,并注意编译器告诉你的内容!
  • @thomasPadron-mccarthy 它使用 g++ 在 linux 终端中编译和运行?它会为你运行
  • g++ 是 C++ 编译器,而不是 C。

标签: c arrays struct char segmentation-fault


【解决方案1】:

修复一些错误后,代码编译,你会从编译器收到几个警告。其中之一是:

foo.c:183:18: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
         printf("%s",postfix[x]);
                 ~^  ~~~~~~~~~~
                 %d

您使用了错误的格式说明符。 postfix[x] 是一个字符(在这种情况下被转换为整数,因此是关于“int”的警告),而不是一个字符串。你应该使用"%c" 而不是"%s"。

进行此更改后,程序在我运行时不再崩溃。

【讨论】:

  • 感谢您指出这一点。不幸的是,我的代码在该行执行之前遇到了分段错误。所以它仍然给我分段错误的错误。
  • 程序不会为我崩溃。但是你检查过其他警告吗?关于 sizeof?
  • Thomas pador-mccarthy 是的,我正在尝试一种新的条件来遍历中缀[]
猜你喜欢
  • 2023-02-01
  • 2017-12-05
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 2021-12-02
  • 1970-01-01
  • 2020-07-23
相关资源
最近更新 更多