Code stack and pop a stack

Asked

Viewed 3,323 times

1

People I’m testing this code in codeblocks and it’s giving a very weird result, saying that the stack is empty and I can’t see where the error is.

void empilha (int *p[], int *topo, int valor){
    if (*topo<10)
    {
        p[*topo] = valor;
        *topo++;
    }
    else
        printf ("Pilha cheia!");
}

int desempilha (int *p[], int *topo){
    if (*topo>0)
    {
        *topo--;
        return *p[*topo];
    }
    else
        printf("Pilha vazia!");
}


int main() {

    int pilha[10], topo=0;
    int i, valor=1020;
    for (i=0; i<10; i++)
    {
        empilha(pilha, &topo, valor);
        valor = valor + i;
    }

    for (i=0; i<10; i++)
    {
        printf("&d\n", desempilha(pilha, &topo));
    }
}

1 answer

5


The error is found in this excerpt:

void empilha (int *p[], int *topo, int valor) {
    if (*topo<10)
    {
        p[*topo] = valor;
        *topo++; // note essa declaração
    }
    else
        printf ("Pilha cheia!");
}

When you have the expression *topo++, you are actually increasing the value of the pointer and afterward applying the dereference operator * (see the precedence of operators). Thus, the value top is never incremented.

To express, in fact, what you want, you must use (*topo)++. Thus, you deselect the pointer and then increment the value pointed by it.

Note that the error also occurs in the pop-up function:

int desempilha (int *p[], int *topo){
    if (*topo>0)
    {
        *topo--; // aqui, o ponteiro é decrementado antes da desreferência
        return *p[*topo];
    }
    else
    printf("Pilha vazia!");
}

In this case, to decrease the value top, use the expression (*topo)--.

There is yet another error in the code posted. The functions empilha(int *p[], int *topo, int valor) and desempilha(int *p[], int *topo) request as first parameter a pointer to an array, which could be written as int **p.

However, within the method main, you are passing as argument for these functions the variable int pilha[], which is an array, ie an int *.

You must change the arguments int *p[] for int p[] (or int *p), in addition to changing the function return expression desempilha for return p[*topo]; for compliance with the previous change.

Finally, note that the function formatting character printf below should be %d, nay &d.

for (i=0; i<10; i++)
{
    printf("&d\n", desempilha(pilha, &topo)); // ao invés de &d, o correto é %d
}
  • Opa thanks, but here it did not work, I put the parentheses in top-- and top++ and ran the program, gave a Windows error ("... Stopped working - debug or close program".

  • 1

    Note that its functions empilha() and desempilha expect a parameter int *p[], that is, a pointer to a vector, and you’re passing them a vector (and treating it as a vector in the function body). Try saying (int p[], ...

  • 1

    Besides, you also need to say return p[*topo]; in place of return *p[*topo]; to avoid reading random memory addresses

  • 1

    @Wtrmute added the information regarding this other problem in the answer.

  • I switched the int arguments *p[] to int p[] I took the pointers before p and the result was '&d' below each other 10 times.

  • 1

    Maybe it’s because the right one is %d and not &d.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.