Search code examples
cheadergcc-warning

Assignment makes pointer from integer warning


Header:

#ifndef BIT_H_INCLUDE_GUARD
#define BIT_H_INCLUDE_GUARD

typedef unsigned char byte;

typedef struct{
    size_t* size;
    byte* map;
} bit;

bit* bdcteate(byte* size);

#endif /* BIT_H_INCLUDE_GUARD */

source:

#include <stdlib.h>
#include "bit.h"

bit* bdcreate(byte* size){
    bit* d;
    byte i;
    size_t s = 0;
    for(i = 1; i < size[0]; i++){
        s += (size_t) size[i];
    }
    if(!(d = malloc(sizeof(bit)))){
        return (bit*) NULL;
    }
    if(!(d->size = malloc(sizeof(size_t)))){
        return (bit*) NULL;
    }
    if(!(d->map = malloc(s * sizeof(byte)))){
        return (bit*) NULL;
    }
    *d->size = s;
    return (bit*) d;
}

main:

#include <stdlib.h>
#include "bit.h"

void main(void){
    byte *b, i;
    byte size = 9;
    b = malloc((size+1) * sizeof(byte));
    b[0] = size;
    for(i = 1; i <= size; i++){
        b[i] = (b[i-1] + 10);
    }   

    bit* dict;
    if(!(dict = bdcreate(b))){ /* warning: assignment makes pointer from integer without a cast */
        exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
}

As you see in the commented line, I get "warning: assignment makes pointer from integer without a cast" although I try to explicitly state that I am returning a pointer.

I'm compiling with gcc (gcc -o test main.c bit.c) and the program seems to run OK.

Should I ignore that or is it going to come back and bite me while I sleep?


Solution

  • In your header:

    bit* bdcteate(byte* size);
            ^ this is a typo
    

    Which means the compiler will infer a default signature for bdcreate in your main, and the default signature returns an int.