Stack implementation in C, How to pass values?

I am trying to do the implementation of a stack in C. The requested by the teacher is already basically done which was to show an error if the stack was full or empty. But when going to main I found great difficulty.

//
//  Trabalho Prático 1.c
//  IFTM
//
//  Created by Lelre Ferreira on 4/26/20.
//  Copyright © 2020 Lelre Ferreira. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#define MAX_ELEMENTOS 5

typedef struct {
    //Vetor onde os dados serão armazenados posteriormente
    int elementos[MAX_ELEMENTOS];
    //Posição na pilha e quantidade de elementos
    int topo;
} pilha;

pilha * cria_pilha(){
    pilha *pi;
    pi = malloc(sizeof(pilha));

    if (!pi) {
        pi -> topo = 0;
    }
    return pi;

}

int empilha(pilha *pi, int p);
int desempilha(pilha *pi);
int tamanho (pilha *pi);
void destroi(pilha *pi);

int main (int argc, const char * argv[]){
    //Ponteiro para pi para pilha, sempre que for usar no main
    //Criando a pilha atribuindo ao ponteiro pi a chamada de função cria_pilha();
    pilha *pi = cria_pilha();
    //Cria vetor de struct preenchido com a quantidade MAX_ELEMENTOS
    pilha infos[MAX_ELEMENTOS] = {1, 2, 3, 4, 5};

    for (int i = 0; i < MAX_ELEMENTOS; i++) {
        empilha(pi, infos[i]);
    }
}

int empilha(pilha *pi, int p){
    if (pi == NULL || pi -> elementos == ((int*)MAX_ELEMENTOS)) {
        printf("Erro, pilha cheia.\n");
        return 0;
    }

    pi -> elementos[pi->topo] = p;
    pi -> topo = pi -> topo + 1;
    return 1;
}

int desempilha(pilha *pi){
    if (pi == NULL || pi -> elementos[0] == 0) {
        return 0;
    }

    pi -> topo = pi -> topo -1;
    return pi -> elementos[pi->topo];
}

int tamanho(pilha *pi){
    return pi -> topo;
}

void destroi(pilha *pi){
    free(pi);
}

I created the for inside the main to call the fill function MAX_ELEMENTS times and put the vector values inside the stack. However in for I am getting this error... Passing 'stack' to parameter of incompatible type 'int'

I identified, the error the function stacks, the second argument is as integer. And when I call the function in main, I'm passing the stack creation pointer and an infos vector of type stack and not int. However I created the vector as type stack by the phallus of the struct of the code being declared as typedef struct stack which is a requirement as well... How can I work with this vector?

Author: Lelre Ferreira, 2020-04-29

1 answers

Hello I made a small correction, with this change will be passed as argument an int value of array elements , present in struct info of type stack , instead of struct info itself,

for (int i = 0; i < MAX_ELEMENTOS; i++) {
        empilha(pi, infos -> elementos[i]);
    }

I added this part to test if the Stack had been successfully populated

for (int i = 0; i < MAX_ELEMENTOS; i++) {
        printf("%d", pi -> elementos[i]);
}
 2
Author: willer quintanilha, 2020-04-29 12:02:25