Reputation: 2544
here is code pieces:
ip.h
typedef union _ip_t{
struct _dot_ip {
unsigned char f4;
unsigned char f3;
unsigned char f2;
unsigned char f1; //the first field
}dot_ip;
unsigned int int_ip;
}ip_t;
ip.c
ip_t
get_mask(int sub_len)
{
assert(sub_len > 0 || sub_len < 32);
ip_t ret;
ret.int_ip = ~((1 << (32 - sub_len)) - 1);
return ret;
}
main.c
ip_t mask;
mask = get_mask(24);
then the error:
error: incompatible types when assigning to type ‘ip_t’ from type ‘int’
mask = get_mask(24);
I can't figure out where is wrong, any help will be appreciated
PS: gcc verison: gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
Upvotes: 0
Views: 112
Reputation: 320747
No declaration of your your function is visible in main.c
. In main.c
the function is completely unknown to the compiler. Your compiler assumed that it returns int
. The rest follows.
Such behavior of the compiler is C89/90-specific. It has been outlawed in C99 language specification. Modern C compilers are not supposed to let you call undeclared functions.
Add a prototype of your get_mask
function to ip.h
ip_t get_mask(int sub_len);
to tell the compiler that get_mask
actually returns ip_t
.
Since your are using gcc
, I suspect that the compiler actually issued an additional diagnostic message informing you about get_mask
being undeclared. Did you just ignore that message?
Upvotes: 3