What’s wrong with my dynamic stack?

Asked

Viewed 62 times

1

I’m trying to make a dynamic stack, and for some reason there’s something wrong with the function init. You’re making the following mistake:

Warning: Conflicting types for 'init'

#include <stdio.h>
#include <stdlib.h>

typedef struct TipoCelular
{
    int elemento;
    struct TipoCelular *prox;
}elementoPilha;

typedef struct TipoTopo
{
    elementoPilha *Topo;
}Pilha;

int main()
{
    Pilha *p;

    init(p);

    if(empty(p))
        printf("Pilha vazia\n");

    return 0;
}

void init(Pilha *p)
{
    p->Topo = NULL;
}

int empty(Pilha *p)
{
    if(p->Topo == NULL)
        return 1;

    return 0;
}

1 answer

4


Is because you used the function init() before it is defined.

Put her setting before the function main or leave it where it is and just add her prototype before the main.

void init(Pilha *p);
/* ... */
  • I declared the prototypes, but when compiling gives: . exe stopped working.

  • Now that I understand. The *p in his main is not a Pilha, is only a pointer. Try replacing with Pilha p and when passing it as argument, use init(&p) and empty(&p). Here’s an example: https://repl.it/@wldomiciano/Example-in-C

  • It worked. Thank you!

Browser other questions tagged

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