linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Pointer to a char
@ 2012-09-18  9:29 Randi Botse
  2012-09-18 10:29 ` Phil Sutter
  2012-09-19  1:04 ` Jon Mayo
  0 siblings, 2 replies; 7+ messages in thread
From: Randi Botse @ 2012-09-18  9:29 UTC (permalink / raw)
  To: linux-c-programming

Hi, having coding in C for 3 years but I'm still not clear with this one.
Consider this code.

...
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")
2. Why pointer to char p copied to j correctly, why not every member
in p overflow? since it is a signed char.

Regards.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-09-19 18:09 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-18  9:29 Pointer to a char Randi Botse
2012-09-18 10:29 ` Phil Sutter
2012-09-18 10:33   ` Duan Fugang-B38611
2012-09-19  1:04 ` Jon Mayo
2012-09-19  7:59   ` Randi Botse
2012-09-19  8:47     ` Leon Shaw
2012-09-19 18:09     ` Jon Mayo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).