I was wondering why the following code doesn't compile:
void foo_int(int *a) { }
void foo_long(long *a) { }
int main()
{
int i;
long l;
foo_long(&i);
foo_int(&l);
}
I am using GCC, and neither calls work either in C or C++. Since it is a 32-bit system, both int and long are signed 32-bit integers (which can be verified with sizeof at compile time).
The reason I am asking is that I have two separate header files, neither are under my control, and one does something like: typedef unsigned long u32;
and the other: typedef unsigned int uint32_t;
. The declarations are basically compatible, except when I use them as pointers as in the above code snippet, I have to explicitly cast.
Any idea why this is?
In 32-bit operating systems, the int type is usually 32 bits, or 4 bytes. Thus, the int type is equivalent to either the short int or the long int type, and the unsigned int type is equivalent to either the unsigned short or the unsigned long type, depending on the target environment.
int , long , ptr , and off_t are all 32 bits (4 bytes) in size. int is 32 bits in size.
Just because long
and int
both happen to be 32-bit on your particular compiler and hardware, does not mean that they will always both be 32-bit on every kind of hardware and every compiler.
C (and C++) were designed to be source-portable between different compilers and different hardware.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With