-2

I have a function that takes a float** as an input variable, Jim. And I have another variable that was returned from a function of type void*. How do I cast void* to float**? They should both be pointers to arrays of floats, but why float** a pointer to a pointer? is this correct?

void* Joe;
float** Jim = *(float*) Joe;

From my research casting to float has some restrictions. Like void* can be cast to float. Do I need to pull the address of Joe like this:

float** Jim = *(float*) &Joe;

Isn't Joe an address already as it is a pointer?

izzy
  • 1,309
  • 1
  • 15
  • 25
  • `float*` is a pointer, but where do you store the pointer? What is its address? That's the definition of `float**`, as you assumed, it's a pointer to the spot in memory that is hold the pointer to a float. – scohe001 Jul 29 '14 at 15:20
  • 4
    Well, you'd do `Jim = (float**) Joe;`, but it would be utter nonsense. – Hot Licks Jul 29 '14 at 15:20
  • why is `Jim = (float**) Joe;` utter nonsense? Are you failing to understand that void* is a common way to point to anything? Not a pointer to a void object. – izzy Sep 02 '14 at 19:12
  • avoid `void*` in C++. This is ugly and unsafe C hack. – Walter Sep 02 '14 at 19:26
  • 1
    HAHAHAHAHAHA, Okay, and when I am dealing with C libs that use `void*` I should just break down and cry right. – izzy Sep 02 '14 at 19:38
  • Possible duplicate of [What does "dereferencing" a pointer mean?](https://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean) – jww Mar 14 '18 at 21:35

2 Answers2

4

The cast should be the same type as the variable on the left, so

float **Jim = (float**)&Joe;
dohashi
  • 1,771
  • 8
  • 12
2

It's impossible to give a reasonable answer without knowing what it is you want to achieve.

void* Joe;

means Joe is a pointer to something. We just have no idea what kind of thing it points to.

float** Jim;

means Jim is a pointer. And we know what kind of thing it is supposed to point to: It is supposed to point to float*, that is to another pointer, pointing to a float.

Now you can assign:

Jim = (float **)Joe;

What happens now is that Jim points to whatever Joe was pointing to. We have no idea what Joe was pointing to, but Jim points to the same thing. If Joe pointed to an int, then Jim points to the same int, but the compiler thinks that Jim points to a float*. Since int and float* are very different things, using *Jim will lead to disaster.

If Joe happened to point to a float*, then Jim will now point to the exact same float* and everything is fine. If Joe happened to point to a float (remember a float is not the same as a float * ) or a double* (remember a double* is not the same as a float* ) things will go wrong.

To repeat: The assignment Jim = (float **)Joe; doesn't change what Joe points to. It changes what the compiler thinks Jim is pointing to. If what the compiler thinks Jim is pointing to, and what Jim actually points to, are not the same, then things will go wrong.

Your line

float** Jim = *(float*) &Joe;

is totally wrong. Joe is a pointer to something. We don't know what. &Joe is the address of that pointer, so it is a pointer to Joe, which is a pointer that points to something we don't know (void **). You cast that pointer to float* and that gives a pointer to Joe, but the compiler know thinks that the variable Joe contains a float and not a void*. * (float* ) &Joe; reads the void* stored in the variable Joe, but the compiler thinks it is not a void*, but a float. That couldn't be any wronger. Then you ask the compiler to assign that float value to a float * * which the compiler won't allow, because a float and a float * * are totally different things.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • Thanks for the detailed explanation. You can certainly assign `Jim = (float **)Joe;` but the value Joe points to is not going to be a pointer to a float it is going to be the first float of Joe. So as dohashi answere is correct. – izzy Sep 02 '14 at 19:31