Pointer help

Hello there.
Well i need to convert a float to void *.
And later convert that void * to a float.

But how to do it in c++?


Thanks -Leandro
Please Google it next time.
http://stackoverflow.com/questions/7828393/c-programming-casting-a-void-pointer-to-an-int

http://www.cplusplus.com/doc/tutorial/typecasting/

1
2
3
4
5
6
7
8
    float f{ 1.23f };
    void* pVoid{ &f };
    
    float orig{ *(float*)pVoid };
    
    cout << "f: " << f << endl;
    cout << "pVoid: " << pVoid << endl;
    cout << "orig : " << orig << endl;


f: 1.23
pVoid: 0x731f245004ec
orig : 1.23
Last edited on
Thanks by the answer.

Well, that is almost what i want.
I need to convert a const float value not a variable. 'Cos i can't make a void* to hold a address and later on get the value of the address.

so i need :
1
2
3
4
5
6
7
void* convertV (const float & a) {
     return (/*WHAT*/);
}

float convertF (void* a) {
    return *static_cast<float*>(a);
}
Why do you need this? Working with void pointers is generally a sign of doing something wrong.
Well i want to do a simple project experimenting pointers, and i have a vector of void*
for store any value type, and adding values on it.

So, you said that working with void pointers is something bad. Well can you say why, and if is possible an alternative to void pointer ?
Working with void pointers is bad because you have all the problems associated with pointers in addition to the lack of type safety. You are basically throwing away all the mechanisms in place to ensure type safety and adding in the problems of pointers on top of it. It is extremely rare to actually need to use a void pointer in C++.

An alternative is a typesafe wrapper like boost::any, but it is still uncommon to need this. Why do you need to store objects of different types in the same container?
Last edited on
I'm a C# and Java programmer. So i can use Object class.
And now i'm trying to learn C++. And i want to make a simple program that accepts :
IADD, 1, 1 FADD, 0.2f, 12.45f
And it needs to print in the screen the result. When IADD is integer addition and FADD is float addition. So i need a type that can hold integers and floats and later chars
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
#include <cctype>
#include <functional>
#include <cmath>

template < typename T, typename FN > void execute( std::string op, FN fn = {} )
{
    T a, b ;
    if( std::cin >> a >> b ) std::cout << a << ' ' << op << ' ' << b << " == " << fn(a,b) << '\n' ;
    else
    {
        std::cerr << "invalid input ignored\n" ;
        std::cin.clear() ;
        std::cin.ignore( 1000, '\n' ) ;
    }
}

int main()
{
    std::string instr ;
    while( std::cout << "enter INSTRUCTION ARG1 ARG2: " && std::cin >> instr )
    {
        for( char& c : instr ) c = std::toupper(c) ;

        if( instr == "IADD" ) execute<int>( "+", std::plus<>() ) ;
        else if( instr == "FADD" ) execute<double>( "+", std::plus<>() ) ;
        else if( instr == "CAT" ) execute<std::string>( "+", std::plus<>() ) ;
        else if( instr == "IMUL" ) execute<int>( "*", std::multiplies<>() ) ;
        else if( instr == "FMUL" ) execute<double>( "*", std::multiplies<>() ) ;
        else if( instr == "FPOW" ) { double (*pow)(double,double) = std::pow ; execute<double>( "^", pow ) ;}
        // else if
    }
}

clang++ -std=c++14 -stdlib=libc++ -O3 -Wall -Wextra -pedantic-errors main.cpp
./a.out <<< 'fadd 12.18 6.9 imul 34 -6 fpow 3.56 2.3 cat abcd efg fmul 3.4 0.99'

enter INSTRUCTION ARG1 ARG2: 12.18 + 6.9 == 19.08
enter INSTRUCTION ARG1 ARG2: 34 * -6 == -204
enter INSTRUCTION ARG1 ARG2: 3.56 ^ 2.3 == 18.5496
enter INSTRUCTION ARG1 ARG2: abcd + efg == abcdefg
enter INSTRUCTION ARG1 ARG2: 3.4 * 0.99 == 3.366
enter INSTRUCTION ARG1 ARG2: 

http://coliru.stacked-crooked.com/a/b0bb600587e62cda
Topic archived. No new replies allowed.