void* aPtr = NULL; // we don't yet know what it points to. ... aPtr = &height; // it has the address of height, but no type yet. ... int h = (int)*aPtr; // with casting, we can now go to that address // and fetch an integer value.( Aprende Programación C; Jeff Szuhay )
'con el casting, ahora podemos ir [...]'
La pregunta es: ¿podemos realmente?
Eliminación de referencias y Casting Void Ptr (Aprender programación en C, Jeff Szuhay)
'con el casting, ahora podemos ir [...]'
La pregunta es: ¿podemos realmente?
Sí, pero el código que se muestra no elimina ninguna referencia. Bueno, intenta desreferenciar un void* y convertir el resultado en int . Así no es como se debe hacer. Primero debe convertir a int* y luego desreferenciar ese int* .
int h = *(int*)aPtr; // now dereferenced ok (assuming `height` is an `int`) // ^ ^ // | | // | proper cast // | // dereferencing the right thing