4 ms·
it's a local variable, and local variables do not get initialized in C.
by anywherenotes 13y ago
it's a local variable, and local variables do not get initialized in C.
- jburgueno 13y agoBut the initialization of the variable is right after the memset: address.sin_family = AF_INET; address.sin_addr.s_addr = ((struct in_addr *)(host->h_addr_list[0]))->s_addr; address.sin_port = htons(port); Why is it useful the put 0 before setting those values?
- oh_teh_meows 13y agoIt's good practice in general. There might be other fields that were not explicitly set, or the struct may expand in future versions, or you have union fields/bitfields (e.g. you explicitly set the low order bits of some union field to some value, thinking the high order bits are all zeroes, but because you neglected to zero them out first, they're filled with garbage bits. when you read the union field as a whole you'll get incorrect values).
- jburgueno 13y agoThanks for the explanation!