|
newb confusion about pointers |
William Labbett
Member #4,486
March 2004
|
when using set_color(int index, const RGB *p) does a RGB pointer have to be declared ? ie RGB *black; and then set_color(255, black); i think this must be wrong since it's not working in my code.
|
Elverion
Member #6,239
September 2005
|
Try just &black, without making it a pointer. Can you better describe what is not working? -- |
sj971059
Member #7,689
August 2006
|
i think between "RGB *black;" and "set_color(255, black);", this is the syntax of set_clor function: void set_color(int index, const RGB *p); Sets the specified palette entry to the specified RGB triplet. Unlike the other palette functions this doesn't do any retrace synchronisation, so you should call vsync() before it to prevent snow problems. Example: |
Indeterminatus
Member #737
November 2000
|
Well, I'm afraid I can't tell you more than to grab a book or find a tutorial covering pointer basics. There are already plenty of good resources out there, so it won't make much sense if we explained it all over again ;-) Syntactically, your code is correct. The fact that p is passed as const however suggests that it is used as in-parameter (pass-by-reference instead of pass-by-value). The pointer black in your example points to anything - you didn't initialize it, so you must not dereference black, which I suppose set_color does. So, quick fix (be sure to read up on the topic, though!): edit: Beaten. _______________________________ |
William Labbett
Member #4,486
March 2004
|
thanks well basically by not working i mean the program just terminates with no error i've always been a bit flummoxed by pointers. sometimes they make sense, other times not it seems strange - with the functions that use BITMAP pointers makes sense the function argument type is BITMAP *bmp say. BITMAP *my_bmp; and then you just use the name my_bmp i you apply the same logic to the functions which use RGB's you'd expect to have to declare an RGB pointer
|
Tobias Dammers
Member #2,604
August 2002
|
Quote: i've always been a bit flummoxed by pointers. sometimes they make sense, other times not They make sense all the time, just not to you :-) Anyway, I'll explain. In the RGB example you give, the pointer is "abused" for the C-version of pass-by-reference. "Pass-by-reference" means: When passing something to a function, we don't give the function the value ("pass-by-value"); instead, we're telling the function where to find it. Remember that a pointer is an "address" in memory; it points to a memory location. So effectively, what you need to do is give the set_color() function the address of the RGB structure you want to use. There are 2 ways of achieving this: b) Define an RGB struct, fill it with the desired values, pass its address (using the & operator) to the function. By directly initializing the struct, you can make the code more compact:
So why do we pass by reference instead of value? There are 2 reasons why one would do that. One is speed: Anything larger than a pointer can be passed more efficiently by reference; this removes strain from the stack, and requires less memory allocation / deallocation in general. Also, there is no need to copy actual data. --- |
William Labbett
Member #4,486
March 2004
|
so does this mean it's always possible to take the address of a variable foo(&x); even when it's not actually declared as a pointer ?
|
Indeterminatus
Member #737
November 2000
|
Yes. _______________________________ |
William Labbett
Member #4,486
March 2004
|
that's great, it makes sense now i'm on the road to pointer clarity
|
|