Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
C Why can't a void pointer store a float value?

I'm trying to return a float value but when I print it out, I get 0.0.

float floating = 4.5;

void* function(){
    void* test = &floating;
    return test;
}

int _tmain(int argc, _TCHAR* argv[]){
     printf("%f\n", test());
     return 0;
}

Any ideas on why it's not printing out 4.5? Sorry if this is a noob question, I'm still fairly new to this.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Assuming your function() is actually spelled as test(), %f expects a float value. You're supplying a void *. It's wrong and undefined behaviour.

Solution: void being an incomplete type, you cannot dereference a void pointer directly. You need to cast the pointer to the required type before dereferencing. For example,

printf("%f\n", *( (float *) test() ) );
over 4 years ago · Santiago Trujillo Denunciar

0

Try

printf("%f\n", *( (float*)function() ));
over 4 years ago · Santiago Trujillo Denunciar

0

Trying to provide a bit more details about that:

First, from how you phrased your question, you should understand that pointers don't store values, they store addresses of objects. void is no type at all, and a void * pointer might store the address of anything (as long as it's data, but I don't want to overcomplicate my answer right here).

That's probably where you came from in the first place. But then you made two mistakes:

  1. The %f format specifier of printf() doesn't expect a pointer, it expects a value. There's one operation called dereferencing a pointer that fetches the value the pointer points to. In c expressions, you have the dereferencing operator written as an asterisk (*) in front of your value to do that.

  2. So how can you read a value without knowing the type? You can't! That's why dereferencing a void * pointer is illegal. Means, even if you didn't forget the asterisk and wrote for example printf("%f\n", *(test()));, the code would be wrong (and this time, your compiler would catch the error).

That being said, void * pointers in c are meant as a generic pointer and are therefore implicitly convertible to and from any other data pointer type (note this is different in c++). So one way to write your program correctly would be:

int _tmain(int argc, _TCHAR* argv[]){
     float *f = test();
     printf("%f\n", *f);
     return 0;
}

edit: From the fact that void * is implicitly convertible to/from other data pointer types follows your test() could be as simple as that:

void *test()
{
     return &floating;
}

edit2: As you're new to pointers, adding an advice not directly related to your question: In a variable declaration in c, the asterisk binds to the identifier (variable name), not to the type. This means you read int * foo; not as "foo is a variable of type pointer to int" but as "foo is a pointer variable of type int". This has a serious implication when writing e.g. int * foo, bar; -- foo will be a pointer, bar a normal variable. Therefore the advice is: make it explicit by writing e.g. void *test instead of void* test.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda