Vinculación de archivos de encabezado

I built a stack data structure, which has a peek method. The compiler is giving me a warning: initialization makes pointer from integer without a cast warning when calling peek in main. Here is what I have:

stack.h:

struct stack_elem {
    struct stack_elem *next;
};

struct stack {
    struct stack_elem *top;
};

void stack_init(struct stack *stack);
int isEmpty(struct stack *);
struct stack_elem * peak(struct stack *);

Y su implementación:

void stack_init(struct stack *stack) {
    stack->top = NULL;
}

int isEmpty(struct stack *stack) {  
    if (stack->top == NULL) 
        return 1;
    else
        return 0;
}

struct stack_elem * peek(struct stack *stack) { 
    if (isEmpty(stack) == 1)  
        return NULL;
    else
        return stack->top;
}

En mi main.c, I define stack as follows:

struct stack stack;
stack_init(&stack);

and call peek:

struct stack_elem * elem = peek(&stack);

This line throws the warning. The weird thing here is that I used to import stack.c dentro main.c and just compile with $ gcc main.c. This had absolutely no errors. Now I changed the import to stack.h, and I am compiling with $ gcc main.c stack.c which throws the warnings. Running it also segfaults (which it didn't beforehand).

preguntado el 29 de julio de 12 a las 09:07

can you give all source code ? -

why your declaration contains a statement : node->value.character = *p; ? -

@Aftnix sorry I fixed it. I copied it from an emacs split screen and it copied by mistake. -

@laifjei the source code is very long. Would you rather a github link or 3 pastebin links with the code? -

You need to be compiling with more warnings enabled. Your compiler should tell you about undeclared functions — or implicitly declared functions. With gcc, Yo suelo gcc -Wall -Wmissing-prototypes at minimum (usually -Wextra too, and some other rather specific flags to deal with the eccentricities of the old code that I have to work with). This would have told you that peek() was not declared (so it was assumed to be a function returning an int). -

3 Respuestas

Looks like peek() isn't declared in the .h, so the compiler is assuming it's an int. main() is calling it an assuming it's a pointer return type.

The warning is 100% correct!

Respondido 29 Jul 12, 10:07

Seems to me there is a typo in your header file?

struct stack_elem * peak(struct stack *);

"peak", your implementation reads "peek". Don't know if that may cause such a warning, though.

Respondido 29 Jul 12, 10:07

It will cause the warning; peek() is implicitly declared to be a function returning an int, and hence the conversion being warned about occurs. - jonathan leffler

Cambiar:

struct stack_elem * peak(struct stack *);

A:

struct stack_elem * peek(struct stack *);

:)

Respondido 29 Jul 12, 10:07

Damn it - mistakes like these make me want to bang my head to the wall 10 times. It was working all long when it shouldn't have. Thanks a lot! I guess it needed a fresh pair of eyes. - cielo oscuro

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.