Thanks, Phil, It is great for the detail explain. Best Regards, Andy -----Original Message----- From: linux-c-programming-owner@xxxxxxxxxxxxxxx [mailto:linux-c-programming-owner@xxxxxxxxxxxxxxx] On Behalf Of Phil Sutter Sent: Tuesday, September 18, 2012 6:30 PM To: Randi Botse Cc: linux-c-programming Subject: Re: Pointer to a char Hi, On Tue, Sep 18, 2012 at 04:29:32PM +0700, Randi Botse wrote: > ... > char *p; > unsigned int i = 0xcccccccc; > unsigned int j; > > p = (char *) &i; > printf("%.2x %.2x %.2x %.2x\n", *p, p[1], p[2], p[3]); > > memcpy(&j, p, sizeof(unsigned int)); > printf("%x\n", j); > ... > > Output: > > ffffffcc ffffffcc ffffffcc ffffffcc > 0xcccccccc > > > My questions are: > > 1. Why it prints "ffffffcc ffffffcc ffffffcc ffffffcc"? (if p is > unsigned char* then it will print correctly "cc cc cc cc") This is because of the two's complement in which singed absolute values are stored internally. Since %x is a conversion of an integer, signed extension of the passed char happens, which in two's complement means that the leading bit is replicated to fill the upper bits. (0xC is 1100 in binary). > 2. Why pointer to char p copied to j correctly, why not every member > in p overflow? since it is a signed char. I am not quite sure about what the question is here (maybe caused by the lack of verbs in your sentence). Keep in mind that memcpy() only copies the memory, irrespective of the pointer type passed. Also, sizeof(unsigned int) == sizeof(int). HTH, Phil -- To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html -- To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html