Reputation: 11
I have this code right here. I'm trying to implement but I got and error and I've not idea how I can get rid of it. It's a stack. I gotta call the function to insert the values in it but the compiler keeps showing this error and don't let me do anything else.
Passing 'pilha' to parameter of incompatible type 'int'
pilha * cria_pilha(){
pilha *pi;
pi = malloc(sizeof(pilha));
if (!pi) {
pi -> topo = 0;
}
return pi;
}
and I'm trying to call it here.
int main (int argc, const char * argv[]){
//Cria vetor de struct preenchido com a quantidade MAX_ELEMENTOS
pilha * pi = cria_pilha();
pilha p[MAX_ELEMENTOS] = {1, 2, 3, 4, 5,
6, 7, 8, 9, 10};
//Call the function Stack, then pass by argument p[]
//stacking the values MAX_ELEMENTS times.
for (int i = 0; i < MAX_ELEMENTOS; i++) {
//The error happens here. I already identified the cause but
//I've got no idea how to solve it. Any help please?
empilha(pi, p[i]); -- Passing 'pilha' to parameter of incompatible type 'int'
}
for (int i = 0; i < MAX_ELEMENTOS; i++) {
desempilha(pi);
}
}
I'm calling the function int empilha(pilha *pi, int p);
which, by my understanding has pilha *pi that is no compatible with the type of int. How can I solve this issue?
Here's my whole code.
#include <stdio.h>
#include <stdlib.h>
#define MAX_ELEMENTOS 10
typedef struct {
int elementos[MAX_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[]){
//Cria vetor de struct preenchido com a quantidade MAX_ELEMENTOS
pilha * pi = cria_pilha();
pilha p[MAX_ELEMENTOS] = {1, 2, 3, 4, 5,
6, 7, 8, 9, 10};
//Chama função empilhar, passa por argumento o velho p[]
//para função empilha MAX_ELEMENTOS vezes.
for (int i = 0; i < MAX_ELEMENTOS; i++) {
empilha(pi, p[i]);
}
for (int i = 0; i < MAX_ELEMENTOS; i++) {
desempilha(pi);
}
}
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);
}
Upvotes: 0
Views: 59
Reputation: 16540
regarding:
pilha *pi;
pi = malloc(sizeof(pilha));
if (!pi) {
pi -> topo = 0;
}
when the if()
is true, then the call to malloc()
failed and pi
contains NULL.
when the call to malloc()
fails, then pi -> topo = 0;
is trying to write to an address just above address 0x00.
This results in undefined behavior and the assignment should result in a seg fault event.
regarding:
for (int i = 0; i < MAX_ELEMENTOS; i++) {
empilha(pi, p[i]);
}
this code block and the function: empilha()
can be replaced with:
memcpy( pi->elementos, p, MAX_ELEMENTOS * sizeof( int ));
OT: in function: empilha()
, this:
if (pi == NULL || pi -> elementos == ((int*)MAX_ELEMENTOS)) {
printf("Erro, pilha cheia.\n");
return 0;
}
should already have been checked before calling this function.
OT: regarding:
printf("Erro, pilha cheia.\n");
error messages should be output to stderr
, not stdout
. Suggest:
fprintf( stderr, "Erro, pilha cheia.\n");
regarding the expression:
((int*)MAX_ELEMENTOS)
this is the same as:
int *10;
Definitely not what you want and this verifies nothing
Upvotes: 0
Reputation: 310980
In this call
empilha(pi, p[i]);
the second argument has the type pilha
due to this declaration
pilha p[MAX_ELEMENTOS] = {1, 2, 3, 4, 5,
6, 7, 8, 9, 10};
but the function expects an argument of the type int
int empilha(pilha *pi, int p);
Pay attention to that it is a bad idea to name identifiers using non-English words. The code in such a case is unreadable.
Also using initializers without braces to initialize aggregates makes the code error-prone and unclear.
Upvotes: 1