1

Here is my problem : I have a structure :

typedef struct Data_Package{
    int type;
    size_t size;
    void* data;
}Data_Package;

and I have a function : (Edit : Armen Tsirunyan remarks) (it is a .C function)

void myFunc(Data_Package* ioData) {
    ioData->data = malloc(sizeof(double));
    (ioData->data)= (double*)62.5
} //that doesn't work

and the main.cpp where I would like to use my function :

Data_Package* ioData=0;
/* some random operation that change the data,type,... */
myFunc(ioData);

stc::cout << *static_cast<double*>(ioData->data) << std::endl  //I want to display te value data (and not the address)

So I would like to know how to change the value data in my function and then display it in the main.cpp (I need to use pointer and data is void* because it could be double or bool or int or ...)

Thanks a lot.

Drlk.

11
  • Looks like you'd be better off using constructors and boost:variant. Commented Apr 30, 2015 at 13:21
  • I think I can try it but is it as precise as a defined type ? for example I receive a double from another file. If I convert it into a variant will I lose data and precision ? Commented Apr 30, 2015 at 13:34
  • What's the problem now? I see you've changed your question (and invalidated an answer). You shouldn't edit your questions this way (do not change it's contents, just add more information with EDIT section). So, what isn't working? What do you want to do? Commented Apr 30, 2015 at 14:10
  • It is still the same problem : I can't access to the ioData->data value. I have tried the solution proposed and changed the function but it still doesn't compile (problem with ioData->data = (double*)62.5 : cannot convert to a pointer). Commented Apr 30, 2015 at 14:15
  • well, you can't convert double value to pointer (which you are doing with (double*)62.5). @ArmenTsirunyan showed you how to do this - *(ioData->data) = 65.5; .Try it. Commented Apr 30, 2015 at 14:22

1 Answer 1

1

1)

ioData->data

is of type void*. You cannot dereference a void pointer. You should first cast it to an appropriate pointer type, in your case, double

cout << *static_cast<double*>(ioData->data)

2)

*(ioData->data) = 65.5;

Your pointer doesn't point to anything valid yet, you can't dereference it. What you probably want to do is

ioData->data = new double;
*static_cast<double*>(ioData->data) = 65.5

3)

As you can see, dealing with void* is ugly. Do you have any specific reason for not storing a double directly in your struct?

Sign up to request clarification or add additional context in comments.

3 Comments

Humm I wil try the cast but I don't want to create the ioData in my function. I assume that the ioData exist in the main. So I won't have to make "ioData->data = new double" in my function ? I wil try the cast and declare the object in the main.cpp and I come back
I can't use the double because data can be any type (double or int or bool or whatever)
I have tried but the function doesn't compile ... =/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.