S
s0suk3
This code
#include <stdio.h>
int main(void)
{
int hello[] = {'h', 'e', 'l', 'l', 'o'};
char *p = (void *) hello;
for (size_t i = 0; i < sizeof(hello); ++i) {
printf("byte %2zu: <%c>", i, p);
if (!p)
printf(" (null char)");
printf("\n");
}
return 0;
}
produces this output
byte 0: <h>
byte 1: <> (null char)
byte 2: <> (null char)
byte 3: <> (null char)
byte 4: <e>
byte 5: <> (null char)
byte 6: <> (null char)
byte 7: <> (null char)
byte 8: <l>
byte 9: <> (null char)
byte 10: <> (null char)
byte 11: <> (null char)
byte 12: <l>
byte 13: <> (null char)
byte 14: <> (null char)
byte 15: <> (null char)
byte 16: <o>
byte 17: <> (null char)
byte 18: <> (null char)
byte 19: <> (null char)
I'm confused about the int *-to-void *-to char * conversion. The
output shows that ints are four bytes on my machine, and the values in
the initializer 'h', 'e', 'l', 'l', 'o' have the values 104, 101, 108,
108, and 111, respectively, so they're able to be represented as chars
(duh...). But if I'd change the initializer to something like
int hello[] = {1000, 43676, 362, 6364, 2575};
I'd get this output
byte 0: <�>
byte 1: <>
byte 2: <> (null char)
byte 3: <> (null char)
byte 4: <�>
byte 5: <�>
byte 6: <> (null char)
byte 7: <> (null char)
byte 8: <j>
byte 9: <>
byte 10: <> (null char)
byte 11: <> (null char)
byte 12: <�>
byte 13: <â–’>
byte 14: <> (null char)
byte 15: <> (null char)
byte 18: <> (null char)
byte 19: <> (null char)
(In other words, non-printable characters.)
Is some kind of overflow happening when I subscript the char pointer?
Or am I simply getting meaningless values because of accessing a char
pointer that points to something that wasn't a char object?
Sebastian
#include <stdio.h>
int main(void)
{
int hello[] = {'h', 'e', 'l', 'l', 'o'};
char *p = (void *) hello;
for (size_t i = 0; i < sizeof(hello); ++i) {
printf("byte %2zu: <%c>", i, p);
if (!p)
printf(" (null char)");
printf("\n");
}
return 0;
}
produces this output
byte 0: <h>
byte 1: <> (null char)
byte 2: <> (null char)
byte 3: <> (null char)
byte 4: <e>
byte 5: <> (null char)
byte 6: <> (null char)
byte 7: <> (null char)
byte 8: <l>
byte 9: <> (null char)
byte 10: <> (null char)
byte 11: <> (null char)
byte 12: <l>
byte 13: <> (null char)
byte 14: <> (null char)
byte 15: <> (null char)
byte 16: <o>
byte 17: <> (null char)
byte 18: <> (null char)
byte 19: <> (null char)
I'm confused about the int *-to-void *-to char * conversion. The
output shows that ints are four bytes on my machine, and the values in
the initializer 'h', 'e', 'l', 'l', 'o' have the values 104, 101, 108,
108, and 111, respectively, so they're able to be represented as chars
(duh...). But if I'd change the initializer to something like
int hello[] = {1000, 43676, 362, 6364, 2575};
I'd get this output
byte 0: <�>
byte 1: <>
byte 2: <> (null char)
byte 3: <> (null char)
byte 4: <�>
byte 5: <�>
byte 6: <> (null char)
byte 7: <> (null char)
byte 8: <j>
byte 9: <>
byte 10: <> (null char)
byte 11: <> (null char)
byte 12: <�>
byte 13: <â–’>
byte 14: <> (null char)
byte 15: <> (null char)
byte 18: <> (null char)
byte 19: <> (null char)
(In other words, non-printable characters.)
Is some kind of overflow happening when I subscript the char pointer?
Or am I simply getting meaningless values because of accessing a char
pointer that points to something that wasn't a char object?
Sebastian