Here is the code:
char s[8];
int i = 0;
void **p = &s;
for (; i < 8; ++i) {
putchar( ((char*)(*p))[i] );
}
The code above could work but gave some rubbish chars. So what I did was simply initialize s[8]
:
char s[8] = {0};
Then segmentation fault, but if I compile and run it using VC++, it works fine. Weird. Could anybody explain? Thanks!
UPDATE: So many guys said the codes above were stupid... this update is for you. What the original codes look like:
static void*
copy_and_move(void **dst, int dsz, const void **src, int ssz) {
const int sz = ssz > dsz ? dsz : ssz;
memcpy(*dst, *src, sz);
return *dst + sz;
}
Then the calling codes:
char d[10], s[8];
copy_and_move(&d, sizeof(char) * 10, &s, sizeof(char) * 8);