gpt4 book ai didi

c - 为什么将取消引用的指针推送到 Stack 输出 null 在这里?

转载 作者:太空宇宙 更新时间:2023-11-04 02:32:59 24 4
gpt4 key购买 nike

这个程序是为了拆分我输入的字符串,例如 2 + 3 然后它会检测到它是一个中缀表达式,然后转换为后缀,例如 2 3 +(strtok 的分隔符是一个空格,例如:"")。

到目前为止,我的问题是,每当我尝试将一个元素插入我的运算符堆栈 (opStack) 然后打印出它应该存储在其中的相应元素时,程序输出它一片空白。

换句话说,我的推送功能无法正常工作,我怀疑这与我尝试将 (&token) 传递给函数然后在 (推)

在这一点上有点难过,所以我们非常乐意提供任何帮助。

#include <stdio.h>
#include <string.h>
#include <ctype.h>

char opStack[5]; // Global operator stack
char operandStack[6]; // Global operand stack
char outputArray[25]={'\0'}; // 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] = {'\0'}; // Initializing character array to NULL
int notation;
int expr_length, i;
expression[25] = '\0';
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: %d\n", top);
printf("Element in opStack[0] is: %s\n", opStack[top]);
if(top == -1)
{
printf("Stack is empty\n");
}
}
void strSplit(char input[25])
{
const char s[2]= " ";
char *token;
token = strtok(input, s);

while(token != '\0')
{

if(*token == '+' || *token == '-' || *token == '*' || *token == '/' || *token == '^' || *token == ')') // If the token is an operator it will be pushed to stack
{
printf("operator is: %s\n", token);
push(&token);
}
else
{
printf("numbers are: %s\n", token);
}
token = strtok('\0', s);
}
}

最佳答案

  • 您的push(char* operator2store)char*作为参数
  • 您的 token 是一个char *

当你传入 &token 时,你传递给 inn 一个 char** ,这应该会给你一个编译器警告。 token 已经是正确的类型,您只需要做:

 push(token);

关于c - 为什么将取消引用的指针推送到 Stack 输出 null 在这里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40956798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com