C-为什么在此处推出一个指针指针堆叠输出null



此程序是为了拆分我要输入的字符串,例如 2 3 ,然后它将检测到它是一个infix表达式,然后转换为Postfix示例 2 3 (Strtok的定界符是一个空的空间:so:" )。

到目前为止,我的问题是,每当我尝试将元素推入操作员堆栈(OPSTACK)>,然后打印出应该存储在应该存储的相应元素时,程序输出的程序输出一片空白。

换句话说,我的推动功能无法正常工作,我怀疑这与我试图通过(&amp; token)>将其与该功能有关,然后在<strong中将其解释。>(push)。

在这一点上有点困难,因此任何帮助都会有很大的义务。

#include <stdio.h>
#include <string.h>
#include <ctype.h>
char opStack[5]; // Global operator stack
char operandStack[6]; // Global operand stack
char outputArray[25]={''}; // Output array
int top = -1;
int endOfArray = 25;
int determine_notation(char input[25], int length);
void push(char* operator2store);
void pop(char operator2store);
void strSplit(char input[25]);
int main(void)
{
   char expression[25] = {''}; // Initializing character array to NULL
   int notation;
   int expr_length, i;
   expression[25] = '';
   printf("Please enter your expression to detect and convert it's notation: ");
   fgets(expression, 25, stdin);
   expr_length = strlen(expression); // Determining size of array input until the NULL terminator
   notation = determine_notation(expression, expr_length);
   strSplit(expression);
}
void push(char* operator2store)
{
    opStack[top++] = *operator2store;
    printf("top value is: %dn", top);
    printf("Element in opStack[0] is: %sn", opStack[top]);
    if(top == -1)
    {
    printf("Stack is emptyn");
    }
}
void strSplit(char input[25])
{
   const char s[2]= " ";
   char *token;
   token = strtok(input, s);
   while(token != '')
   {
      if(*token == '+' || *token == '-' || *token == '*' || *token == '/' ||  *token == '^' || *token == ')') // If the token is an operator it will be pushed to stack
      {
         printf("operator is: %sn", token);
         push(&token);
      }
      else
      {
         printf("numbers are: %sn", token);
      }
      token = strtok('', s);
   }
}
  • 您的push(char* operator2store)char*作为参数
  • 您的tokenchar *

当您通过&token时,您将通过旅馆A char**,这应该给您编译器警告。token已经是正确的类型,您只需要做:

 push(token);

谢谢大家,它终于奏效了。

我必须像nos所说的 push(token)那样传递令牌我的Lifo堆栈的顶部。

您的堆栈算法被打破。

opStack[top++] = *operator2store;            // << Store value at index n
printf("top value is: %dn", top);           // << print n+1
printf("Element in opStack[0] is: %sn", opStack[top]); // << print element at index n+1
if(top == -1)  
// While pushing on the stack, how could it get emtpy? 
// Checking for stack full woule be better.

除了在实际数组(int top=-1;)开始之前,您还弄乱了堆栈指针。

最新更新