>I suspect this goes doubly for trying to get the size of something a void pointer points at.
It does, in a way. If you try to get the size of a void pointer then you will simply get the size of a pointer, but if you cast and dereference a void pointer then sizeof will return the size of the data type you cast the void pointer to. Pretty straight forward in it's own twisted way, but what's really interesting is when you dereference a void pointer to a char pointer you get the size of a char and not a char pointer. Here's a fun little test to see just what can happen:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ( void )
{
char c = 'a', *cp;
double f = 1.2f, *fp;
void *vf, *vfp, *vc, *vcp;
fp = malloc ( sizeof ( double ) );
cp = malloc ( 10 * sizeof ( char ) );
if ( fp != NULL && cp != NULL ) {
*fp = 2.3f;
strcpy ( cp, "Test" );
vf = &f;
vfp = fp;
vc = &c;
vcp = cp;
printf ( "Test 1: %u %u\n", sizeof f, sizeof *fp );
printf ( "Test 2: %u %u\n", sizeof f, sizeof fp );
printf ( "Test 3: %u %u\n", sizeof vf, sizeof vfp );
printf ( "Test 4: %u %u\n", sizeof *(double *)vf, sizeof *(double *)vfp );
printf ( "Test 5: %u %u\n\n", sizeof *(float *)vf, sizeof *(float *)vfp );
printf ( "Test 6: %u %u\n", sizeof c, sizeof *cp );
printf ( "Test 7: %u %u\n", sizeof c, sizeof cp );
printf ( "Test 8: %u %u\n", sizeof *(char *)vc, sizeof *(char *)vcp );
printf ( "Test 9: %u %u\n", sizeof *(double *)vc, sizeof *(double *)vcp );
printf ( "Test 10: %u %u\n", sizeof *(char *)vc, sizeof *(char *)vcp );
free ( fp );
free ( cp );
}
return 0;
}
-Prelude