Ok so I'm building a simple dns client program (without the use of things like getaddrinfo) in C, but I'm getting a weird issue when I print out the response address. The following code correctly prints out addr = 130.127.69.75
, however, when I comment out the memcpy call and uncomment the line after it, my output becomes addr = 4294967170.127.69.75
temp[0] = resp[j++];
temp[1] = resp[j++];
memcpy(&response.rdlength, temp, 2);
printf("resp.rdlength = %d\n", response.rdlength);
response.rdlength = ntohs(response.rdlength);
for(i = 0; i < response.rdlength; i++) {
temp[i] = resp[j++];
}
memcpy(&response.Addr, temp, response.rdlength);
response.Addr = ntohl(response.Addr);
unsigned int *a = &response.Addr;
unsigned int *b = (unsigned int *)malloc(sizeof(unsigned int) * 4);
printf("addr = %x\n", *a);
printf("temp = %c\n", temp[0]);
for(i = 0; i < 4; i++) {
b[i] = 0;
memcpy(&b[i], &temp[i], 1);
//b[i] = (unsigned int)temp[i];
}
printf("addr = %u.%u.%u.%u\n", b[0], b[1], b[2], b[3]);
// exit(2);
I'm just really not sure why that commented out assignment statement gives such a bizarre output, and would appreciate some clarification on why I might be seeing that behavior, and how I can get around it (without memcpy). I can post more of the code if necessary, but I think anything above what I've shown is just a lot of parsing other parts of the response packet, so I haven't initially included it.