# Assigning a value (with a poitner twist)

This topic is 4881 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Greetings, I've come across very odd behavior when assigning a value to a variable, and was hoping someone here might have some insight. Here's the scenario: I've got an array of int pointers, pointed to by "int** ppValues". The the first twist is that I need to be able to store both ints and floats within the same array. It's not really a problem, just requires some casting and makes the code somewhat... ugly. The problem occurs when I go to assign the value pointed to by a pointer in the array. Here's the line that's causing the odd behavior:
*((float*)ppValues[index]) = <float value>;
In the setup I have now, the array of pointers has a size of 3. When the line above is executed, the value pointed to by all 3 pointers in the array now points to a value of <float value>. It also seems to be independant of what 'index' is set to. How is this possible? I'm using MSVS 6.0.

##### Share on other sites
Are you sure that they're actually pointing to separate addresses?

You know, unions can make this kind of thing a lot easier..

##### Share on other sites
The problem is in the setup. You have all 3 pointers pointing to the same thing.

I second the motion to use unions.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634135
• Total Posts
3015752
×