I'm studying socket programming and I learned inet_addr
function. But I'm confused how can I handle in_addr_t
type.
inet_addr
function returns in_addr_t
type which is uint32_t
, then do I have to use uint32_t
type variable to handle it?
In the book, example handles it as unsigned long
type, but I don't understand why this way is used.
unsigned long conv_addr = inet_addr(addr1); // unsigned long == uint32_t?
You do not have to use uint32_t
to handle the value returned from inet_addr
. You can use any type that can represent any value that might be returned or that you might use in a calculation. But why not use the uint32_t
or the in_addr_t
type?
You may use long type also for inet_addr() function since most of the architecture use 32 bits(4 bytes) for long type but it is not always applicable
Some architecture use 64 bits for long type.. LP64 convention use 64 bits for long type
But the size of uint32_t is always 32 bits independent of convention the compiler is following.
If you are writing programs using MSVC, it uses 32 bits for long type.
For gcc, it depends on computer hardware and implementation of GCC.
It is safe to use unsigned long for assigning to the return type of inet_addr() when you are confident with long size is 32 bits.
Recommended to use uint32_t (aka unsigned 32 bit memory) for inet_addr(), so you don't need to care about the size of long type at all...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.