Polish calculator algorithm and pointers

0

I have a small problem in terms of pointers to use the inverse Polish calculator algorithm. Here I share the code:

//Dado un arreglo de caracteres que representan las componentes
//de una expresion en notacion polaca inversa, evalua dicha
//expresion.
//Apila los caracteres numericos en una pila, y cuando encuentra un
//operador matematico, desapila los numeros anteriores y ejecuta esa
//operacion. Apila el resultado.
double calculadora_polaca(char** arreglo, size_t m){
    pila_t* pila = pila_crear();
    if(!pila) return 0.0;
    for(size_t i=0; i<m; i++){
        char* actual = arreglo[i];
        if(isdigit(actual)) pila_apilar(pila,actual);
        else if(es_operador(actual)){
            double resultado;
            char operador = obtener_operador(actual);
            double operacion1 = (*((double*)pila_desapilar(pila)));
            double operacion2 = (*((double*)pila_desapilar(pila)));
            switch(operador){
                case '+':
                    resultado = operacion1+operacion2;
                    break;
                case '-':
                    resultado = operacion1-operacion2;
                    break;
                case '*':
                    resultado = operacion1*operacion2;
                    break;
                case '/':
                    resultado = operacion1/operacion2;
                    break;
                default:
                    printf("ERROR: comando desconocido %c\n",operador);
            }
            pila_apilar(pila,&resultado);
        }
    }
    double res_final = (*((double*)pila_desapilar(pila)));
    return res_final;
}

int main(){
    char* arreglo[3] = {'+','4','1'};
    double res =  calculadora_polaca(arreglo,3);
    printf("%f",res);
    return 0;
}

I have the problem, I think, in the function main() . When compiling, it shows me the following error.

calc-pol-inversa.c:74:22: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
  char* arreglo[3] = {'+','4','1'};

3 equal errors for each element of the array. What am I wrong?

    
asked by Lautis1 02.10.2017 в 03:56
source

1 answer

2
char* arreglo[3] = { '+','4','1' };

With that, you create an array with space for 3 pointers to char , or, which is more or less the same, with space for 3 pointers to chains.

It seems to me that what you want to do is a kind of stack with values, to process them. In that case:

char *arreglo[] = {
  "+",
  "1",
  "4"
};

That would create a fix for strings. If you only want individual characters, it would be

char arreglo[] = { '+', '1', '4' };

Look at the quotes in the 2 cases: "" indicates string , while '' indicates character.

Likewise, char * is pointer to string , while char is simply a character .

Also, it is not necessary to indicate the size. The compiler realizes the solito.

    
answered by 02.10.2017 в 05:32