Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » newb confusion about pointers

This thread is locked; no one can reply to it. rss feed Print
newb confusion about pointers
William Labbett
Member #4,486
March 2004
avatar

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
avatar

Try just &black, without making it a pointer. Can you better describe what is not working?

--
SolarStrike Software - MicroMacro home - Automation software.

sj971059
Member #7,689
August 2006

i think between "RGB *black;" and "set_color(255, black);",
u should make sure that what black pointed is a reasonable value ,then u can use it in "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:
RGB rgb;
...
vsync();
set_color(192, &rgb);

Indeterminatus
Member #737
November 2000
avatar

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!):

RGB black = {0, 0, 0};
RGB *pointer_to_black = &black; // initialize pointer with address of black

set_color(255, &black); // need to reference it
set_color(255, pointer_to_black); // no need to reference, already an address

// ^ above two set_color calls do mutually the same

edit: Beaten. :P

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

William Labbett
Member #4,486
March 2004
avatar

thanks

well basically by not working i mean the program just terminates with no error
message.

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.
it's used by declaring a BITMAP pointer;

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
and just supply it's name like with the BITMAP example

???

Tobias Dammers
Member #2,604
August 2002
avatar

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:
a) Allocate an RGB pointer, fill it with the desired values, pass it to the function, then deallocate. Manual memory management, in other words.

RGB* black;
black = malloc(sizeof(RGB));
black->r = 0;
black->g = 0;
black->b = 0;
set_color(0, black);
free(black);

b) Define an RGB struct, fill it with the desired values, pass its address (using the & operator) to the function.

RGB black; // note that this is not a pointer yet, it's the actual struct
black.r = 0;
black.g = 0;
black.b = 0;
set_color(0, &black); // &black is a pointer, pointing to black

By directly initializing the struct, you can make the code more compact:

RGB black = { 0, 0, 0 };
set_color(0, &black);

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.
The other reason is to allow the called function to alter data from outside. When passing by value, the function receives a copy of the variable you pass, which remains intact after the function returns. Passing a pointer makes it possible to change the memory it points to.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

William Labbett
Member #4,486
March 2004
avatar

RGB black; // note that this is not a pointer yet, it's the actual struct
black.r = 0;
black.g = 0;
black.b = 0;
set_color(0, &black); // &black is a pointer, pointing to black

so does this mean it's always possible to take the address of a variable
e.g
int x;

foo(&x);

even when it's not actually declared as a pointer ?

Indeterminatus
Member #737
November 2000
avatar

Yes.

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

William Labbett
Member #4,486
March 2004
avatar

that's great, it makes sense now :)

i'm on the road to pointer clarity

Go to: