我正在尝试将 char 附加到 C 中的字符串,但它不断在我正在使用的函数中抛出 EXC_BAD_ACCESS 代码 2 错误
void append(char s[], char c)
{
char str[strlen(s)+1];
strcpy (str,s);
char x[2];
x[0]=c;x[1]='\0';
strcat(s, x);
}
调用函数,如您所见,它用于从中缀方程创建后缀方程
const char * postFix(char x[]){
PtrToNode P = malloc(sizeof(struct Node));
P->Next=NULL;
char* m="";
char open='(',close=')';
for (int i=0;i<strlen(x);i++){
if (x[i]!='+'&&x[i]!='-'&&x[i]!='*'&&x[i]!='/'&&x[i]!='%'&&x[i]!='('&&x[i]!=')'){
if (x[i+1]!='+'&&x[i+1]!='-'&&x[i+1]!='*'&&x[i+1]!='/'&&x[i+1]!='%'&&x[i+1]!='('&&x[i+1]!=')'){
append(m, open);
append(m, x[i]);
append(m, x[i+1]);
append(m, close);
}
else
append(m, x[i]);
}
else {
if (x[i]=='(')
Push(x[i], P);
else if (x[i]==')'){
PtrToNode PTN= P->Next;
char ch=Pop(P);
while (ch!='(') {
append(m,PTN->Element);
ch=Pop(P);
}
}
else {
char peak=Peak(P);
if (peak=='+'||peak=='-'){
if (x[i]=='*'||x[i]=='/'||x[i]=='%'){
Push(x[i], P);
}
else {
char whatever = Pop(P);
Push(x[i], P);
append(m,whatever);
}
}
else {
if (peak=='*'||peak=='/'||peak=='%'){
char whatever = Pop(P);
append(m,whatever);
Push(x[i], P);
}
}
}
}
}
printf("%s",m);
return m;
}