I am writing a function which takes in the integer value and the pointer to a character. the function converts the integer value into binary and stores it in the char pointer. the char pointer is 16 bytes long.
Snippet of code:
void int2bin(u_int16_t addr_IP, char *Binary)
{
int count;
printf("IP1add = %d \n", Binary);
for (count = 0; count < 16; count++) {
if(addr_IP>0)
*(Binary + 15-count) = addr_IP & 0x1 ? '1':'0';
else
*(Binary + 15-count) = '0';
addr_IP>>=1;
}
}
int main(int argc, char *argv[])
{
u_int16_t senderIP_16[], u_int16_t receiverIP_16[];
char sender_IP_hi[16], sender_IP_low[16];
int2bin(senderIP_16[0], &sender_IP_hi);
int2bin(senderIP_16[1], &sender_IP_low);
}
In the first call to the function, it returns correct values. But in the second pass, the value of first pass is appended to the second pass, i.e length of sender_IP_low becomes 32.
How can I resolve this?
Thanks
main? 2/ why the length of arrays are not specified?