0

Can someone please explain to me why the following code will compile fine when it is all in the one C file, but when i put the make_queue_data() function into another C file and compile it, it gives me an "assignment makes pointer from integer without a cast" warning?

#include <stdlib.h>
#include <stdio.h>

typedef struct pqueue_data_t
{
    int priority;
    void *queue_data;
} pqueue_data_t;

void*
safe_malloc (size_t size)
{
    void *mem_block = NULL;
    if ((mem_block = calloc (1, size)) == NULL) {
        fprintf (stderr, "ERROR: safe_malloc() cannot allocate memory.");
        exit (EXIT_FAILURE);
    }
    return (mem_block);
}

pqueue_data_t * 
make_queue_data(void *data, int priority)
{
    pqueue_data_t *pdata;
    pdata = (pqueue_data_t *) safe_malloc(sizeof(pqueue_data_t));
    pdata->priority = priority;
    pdata->queue_data = data;
    return (pdata);
}

int *
alloc_data (int val)
{
    int *rv = (int *)safe_malloc(sizeof(int));
    *rv = val;
    return (rv);
}

int
main (int argc, char **argv)
{
    pqueue_data_t *temp;
    temp = make_queue_data(alloc_data(34), 0); /* problem line */
    printf("%d\n", *((int *)temp->queue_data));
    return EXIT_SUCCESS;
}

this isnt the whole of my code, i just cut and pasted the relevant parts into the one.

any help would be greatly appreciated, as i have been bashing my head against this wall for a couple of hours trying to find where the problem is..

guskenny83
  • 1,321
  • 14
  • 27
  • 4
    [Please don't cast the return value of `malloc()` (or your workalike clone) in C](http://stackoverflow.com/a/605858/28169). – unwind Oct 07 '13 at 14:20
  • You're showing us code that compiles, and asking about other code that doesn't compile. We can only guess what that other code looks like. – Keith Thompson Oct 07 '13 at 15:22

2 Answers2

3

It might be (and here I'm guessing) because you don't have a prototype of safe_malloc in the other source file.

Or it might be because you don't have the pqueue_data_t defined in the other source file (it should probably be in a header file instead).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Make sure the function prototype is available in all source files that use the function. If you define a function in one file and use it in another the other file still needs to know the function prototype.

When the compiler sees a function-call that it knows no prototype or definition of it assumes the function to have a return type int, when you assign this to a pointer the compiler warns you about it. This is also the reason you should never cast the result of malloc in case you forget to include stdlib.h.

Kninnug
  • 7,992
  • 1
  • 30
  • 42
  • thanks, i have changed the cast from malloc, im not sure why i did that in the first place actually.. with the other one, the function prototypes for safe_malloc() and make_queue_data() are all actually in header files that are #included in the test source file, but it still is giving me the warning, should i cut-and-paste them into the actual test file itself? – guskenny83 Oct 07 '13 at 14:34