C-分段错误试图将堆栈的顶部放入Char数组

时间:2019-02-06 19:34:23

标签: c arrays struct char segmentation-fault

我的代码出现分段错误,无法解释原因。我正在编写一个程序,该程序使用一个中缀表达式,并使用一个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,并将其存储在infix []

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;   


}

我的下一个功能是对中缀表达式进行排序并将其放入后缀[]

   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++;
}

Postfix [k] = s1.data [s1.top];在while循环中工作较早,但不在这里。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

修复了一些错误之后,代码得以编译,您将从编译器获得一些警告。其中之一是:

andromeda_yyyymmddxls

您使用了错误的格式说明符。 @echo off setlocal enabledelayedexpansion for /f "tokens=1* delims=_" %%i in ('dir /b /a-d andromeda_*.*.*.xlsx') do ( set "manip=%%~nj" if exist "%%i_20!manip:~6,2!!manip:~0,2!!manip:~3,2!%%~xj" ( echo file %%i_20!manip:~6,2!!manip:~0,2!!manip:~3,2!%%~xj already Exists! ) else ( rename %%i_%%j %%i_20!manip:~6,2!!manip:~0,2!!manip:~3,2!%%~xj ) ) 是一个字符(在这种情况下将转换为整数,因此是有关“ int”的警告),而不是字符串。您应该使用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]

更改后,运行该程序时,该程序不再崩溃。