3
I am trying to implement a stack in C. The prompted 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 MAX_ELEMENTOS fill function times and place the array values inside the stack. However I am not 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 am passing the stack creation pointer and an Info vector of the cell type and not int. However I created the array as stack type by code struct phallus being declared as typedef struct stack which is a requirement too... How can I work with this vector?