Skip to main content
gamedev.net
🔒 Locked

problem with const pointers

Started by master_ball 4 replies 300+ views
Original Post
master_ball
master_ball
Hi! I''m trying some OpenGL in linux and have defined some simple colors and a current value. They are all globals.
// colors
const float Black[]	= { 0.0, 0.0, 0.0 };
const float Red[]	= { 1.0, 0.0, 0.0 };
const float Green[]	= { 0.0, 1.0, 0.0 };
const float Blue[]	= { 0.0, 0.0, 1.0 };
const float Magenta[]	= { 1.0, 0.0, 1.0 };
const float Cyan[]	= { 0.0, 1.0, 1.0 };
const float Yellow[]	= { 1.0, 1.0, 0.0 };
const float White[]	= { 1.0, 1.0, 1.0 };

const float currentColor[] = White;
 
I am declaring or assigning the currentColor value wrong. I want to be able to change my currentColor in one of my callback functions or even my main function. Does anyone know where I am going astray? Thanks!! -- master_ball -- ------------------------------ We R 138
-- master_ball --------------------------------student seeking future game dev career, advice welcome
danielf
danielf
You could declare currentColor as a pointer to float...

float *currentColor=White;

If you want to change the currentColor to red, just do:

currentColor=Red;
master_ball
master_ball
Thanks for the reply~!

I gave this a shot but its giving me an invalid initializer on that line when I compile:


// global colors
const float Grey[] = { 0.5, 0.5, 0.5 };
const float Red[] = { 1.0, 0.0, 0.0 };
const float Green[] = { 0.0, 1.0, 0.0 };
const float Blue[] = { 0.0, 0.0, 1.0 };
const float Magenta[] = { 1.0, 0.0, 1.0 };
const float Cyan[] = { 0.0, 1.0, 1.0 };
const float Yellow[] = { 1.0, 1.0, 0.0 };
const float White[] = { 1.0, 1.0, 1.0 };
const float Orange[] = { 1.0, 0.5, 0.0 };
const float Purple[] = { 0.4, 0.0, 0.7 };
const float Black[] = { 0.0, 0.0, 0.0 };

float *currentColor[] = White;


I''m using g++ if that helps any :s
Thanks!
-- master_ball --------------------------------student seeking future game dev career, advice welcome
Blabberboy [disabled]
Blabberboy [disabled]
I''m not entirely sure about this, but probably since your color arrays are all const, if you point currentColor to one of them, that memory is still const, so you shouldn''t be able to change it... (I know... my explanation sucks...)

anyways, since more than likely that little blurb makes next to no sense whatsoever, here''s a possible solution

float* currentColor = const_cast( White );


dunno if that''ll actually work, but it might... other than that, I think you''d just have to make currentColor a separate array in memory and copy values into it

float currentColor[3];
memcpy( currentColor, Magenta, 3 * sizeof( float ) );

or whatever...
danielf
danielf
quote:
float *currentColor[] = White;


Try:

float *currentColor=White;

without the [].

[edited by - danielf on January 31, 2004 5:56:16 PM]
master_ball
master_ball
wooot! finally got it. I needed a pointer to a constant float:


.
.
.
const float White[] = { 1.0, 1.0, 1.0 };
const float Orange[] = { 1.0, 0.5, 0.0 };
const float Purple[] = { 0.4, 0.0, 0.7 };
const float Black[] = { 0.0, 0.0, 0.0 };

float const *currentColor = White;


lol mixing const and pointers is just coming back to me know. Thanks guys!!





-- master_ball --

------------------------------
We R 138

-- master_ball --------------------------------student seeking future game dev career, advice welcome

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.