1

I've got this sort of calculator, that operates with astronomical numbers, this is a code fragment of the menu. What I need to accomplish is to pass or initialize that result pointer declared in main to the functions inside the menu.

The problem is that if I initialize it, for example, in the function realizarSuma(), after each iteration of the while loop it gets set to NULL again, so I can never use that result in another operation later.

int main() {
    NumeroAstronomico *result = NULL;
    int opcion;
    int finaliza = 0;

    while (finaliza == 0) {
        printf("1.Sumar valores\n2.Verificar igualdad de dos numeros\n3.Verificar menor valor\n"
               "4.Guardar resultados\n5.Cargar resultados\n6.Salir\n\n");
        printf("Opcion:");
        scanf("%d", &opcion);
        finaliza = menu(opcion, result);
    }

    return 0;
}

int menu(int opcion, NumeroAstronomico *result) {

    switch (opcion) {
        case 1:
            result = realizarSuma();
            printf("result %s\n", result->entero); //I get the correct result
            return 0;
        case 2:
            verificarIgualdad();
            return 0;
        case 3:
            obtenerMenor(result);
            return 0;
        case 4:
            printf("result %s\n", result->entero);
            guardarResultado(result);
            return 0;
        case 5:
            result = cargarResultado();
            return 0;
        case 6:
            exit(0);
        default:
            printf("Opcion Invalida, ingrese nuevamente\n");
            return 0;
    }
}

Why is this happening and how can I avoid this behaviour?

  • The variable `result` in `menu` is a **copy** of `result` in `main`. It's **not** the same variable, even though the name is the same. Changing `result` in `menu` has no effect on `result` in `main`. You either need to return `result` from `menu`, or you need to declare `result` in `menu` as a pointer-to-a-pointer (`NumeroAstronomico **result`), and then *pass the address* of `result` to the function, e.g. `menu(opcion, &result)` – user3386109 Oct 04 '20 at 16:50
  • Is it really fair to call this a duplicate? The answer might be the same answer to another question but the OP didn't ask, "how do I modify a pointer passed into a function?" He simply didn't know that he needed to pass in a pointer to a pointer. He wanted help figuring out what he was doing wrong. – shrewmouse Oct 04 '20 at 17:03
  • @shrewmouse Yes, that's exactly what happened. When I searched for an answer before asking my own question I did't know many of the questions already answered would solve my problem – Paula Chittaro Oct 04 '20 at 17:28

1 Answers1

0

So this is a time when you want a pointer to a pointer (i.e. double pointer).

int menu(int opcion, NumeroAstronomico **result) {

    switch (opcion) {
        case 1:
            result = &realizarSuma();
            printf("result %s\n", (*result)->entero); //I get the correct result
            return 0;
        default:
            printf("Opcion Invalida, ingrese nuevamente\n");
            return 0;
    }
}

Because what you are really wanting to do is modify the pointer.

shrewmouse
  • 5,338
  • 3
  • 38
  • 43
  • `result` will be dangling after calling menu for case 1... – Deduplicator Oct 04 '20 at 17:05
  • Putting a `&` before the function `realizarSuma()` throws a warning "cannot take the address of an rvalue of type", so I tried to cast it like this `result = (NumeroAstronomico**) realizarSuma();`. But even doing so I get another warning "The value is never used" and again, in the next iteration `result` is NULL – Paula Chittaro Oct 04 '20 at 17:56