我已经为RPN计算器做了一个代码,它对基本运算符(+,*,/,^)以及浮点数和负数都能正常工作。它也评估表达式,如 (x^2 + x * 4/-2):1 - > 5:0.5(x从1到5,步长为0.5)访问冲突初始化结构
我使用了一个char堆栈。
现在,我想添加对诸如cos(x),tan(x)等函数的支持。为了达到这个目的,我需要构建一个char * stack,解析后存储sin,cos,sqrt等单词。
问题是,初始化堆栈时,出现“访问冲突:写入地址0x01”错误。
我不知道为什么。它可能是使用malloc()?
这些是使用堆栈的功能。
typedef struct nodo{
char *operador;
struct nodo *next;
}tipo;
typedef tipo *elemento;
typedef tipo *top;
int push(top*,char*) ;
void init(top *);
void libera(top*);
char* pop(top*);
int main(){
(...)
top op;
init(&op);
(...)
}
void init(top *pila) {
*pila = malloc(sizeof(**pila));
(*pila)->operador = NULL;
(*pila)->next = NULL;
}
void libera(top *pila) {
free(*pila);
*pila = NULL;
}
int push (top *last,char *dato){
elemento new1;
int j=strlen(dato);
new1 = (elemento)malloc(sizeof(tipo));
strncpy(new1->operador, dato,j);
new1->next=*last;
*last=new1;
;}
char* pop(top *last){
elemento aux;
char* caract;
aux = (elemento)malloc(sizeof(tipo));
aux=*last;
if (!aux)
return 0;
*last=aux->next;
strcpy(caract,aux->operador);
free(aux);
return caract;
}
它没有工作,以及,我得到相同的“访问冲突”错误。 谢谢! – Audo
你传给init的是什么?请出示声明。 – n3rd4n1
int main() { (...) top op; 的init(&op); (...) } – Audo