0

C で文字列に char を追加しようとしていますが、使用している関数で 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;
}
4

1 に答える 1