0
EDIT: I’ll leave the codes right below in text same.
Good evening. Please see attached image.
It is the implementation of a function whose prototype is defined in another file named "stack. h". As it stands, it works perfectly.
However, if I delete line 11 and modify line 5 to typedef struct { .... } stack; i have a compilation error:
pile. c: In Function 'breed:
stack. c:15:6: error: Dereferencing Pointer to incomplete type ːstruct pile'
p->v = malloc(tamanho * sizeof(char)); ^~
Could someone help me understand this mistake?
From now on, thank you!
--------------- pile. h -----------------
typedef struct pilha *Pilha;
Pilha criaPilha(int tamanho);
int pilhaCheia(Pilha p);
int pilhaVazia(Pilha p);
void empilha(Pilha p, char elemento);
char desempilha(Pilha p);
int topo(Pilha p);
char eleTopo(Pilha p);
----------- pile. c -----------
#include <stdio.h>
#include <stdlib.h>
#include "pilha.h"
struct pilha {
char *v;
int topo;
int tamanho;
};
typedef struct pilha pilha;
Pilha criaPilha(int tamanho) {
Pilha p = malloc(sizeof(pilha));
p->v = malloc(tamanho * sizeof(char));
p->topo = 0;
p->tamanho = tamanho;
return p;
}
int pilhaCheia(Pilha p) {
if (p->topo > p->tamanho)
return 1;
return 0;
}
int pilhaVazia(Pilha p) {
if (p->topo == 0)
return 1;
return 0;
}
void empilha(Pilha p, char elemento) {
if (!pilhaCheia(p)) {
p->topo++;
p->v[p->topo] = elemento;
}
else
printf("Pilha cheia!\n");
}
char desempilha(Pilha p) {
if (!pilhaVazia(p)) {
p->topo--;
return p->v[p->topo + 1];
}
else
return '\0';
}
int topo(Pilha p) {
return p->topo;
}
char eleTopo(Pilha p) {
return p->v[p->topo];
}
Put the code in text to make it easier for us, and the
.h
.– Maniero
I left the codes in the post Edit.
– Lucas Lopes
I was going to copy and paste your code to change and make an answer, but as picture gets difficult.
– Victor Stafusa
Hi, @Victorstafusa. I edited the post and left the codes in the comments.
– Lucas Lopes