Skip to main content
gamedev.net
🔒 Locked

C question (pointer related)

Started by Pfhorseti 11 replies 300+ views
Original Post
Pfhorseti
Pfhorseti
Say I have a struct like this:
typedef struct
{
    unsigned char		r;
    unsigned char		g;
    unsigned char		b;
    unsigned char		a;
} RGBA;
 
And I want to pass in the address of each element (r, g, b, a) to some function. The function receives the address and stores it into a pointer. The function uses the pointer to store info in r, g, b and a. When I pass the address to the function, how do I express it? Is this the correct way to do it:
&(rgba.r)
 
Or should it be:
&(rgba).r
 
Or neither? Thanks.
-forseti
mmelson
mmelson
It would be &(rgba.r), and I''m 99.9% certain that just &rgba.r works too (but it''s 3:30 am, and I''m not willing to risk saying that I''m sure of it =).
Chem0sh
Chem0sh
&(rgba.r) should do fine. However, since you''re planning on changing all of the members of the struct, why not just pass a pointer to the struct? You''ll have to do rgba->whatever every time you want to change something, but it''ll save you some parameter passing (and save a little space on the stack since you''d only be passing 1 var instead of 4).


/*=========*/
/* Chem0sh */
/*=========*/
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
Chem0sh
Chem0sh
FYI:

&(rgba).r would try to access a member named r in the address of rgba. Your compiler would (should) get VERY upset over this :-D.

mmelson, I think you're right about &rgba.r, but I'm kinda rusty on operator precendence. When in doubt, always use parentheses! :-)

/*=========*/
/* Chem0sh */
/*=========*/

[edited by - Chem0sh on May 24, 2002 3:38:11 AM]
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
Pfhorseti
Pfhorseti
Great, thanks a lot for the quick replies

And I will take that advice of passing a struct pointer.
Thanks again.

[edited by - pfhorseti on May 24, 2002 4:29:44 AM]
-forseti
LessBread
LessBread
quote:
Original post by Chem0sh
mmelson, I think you're right about &rgba.r, but I'm kinda rusty on operator precendence. When in doubt, always use parentheses! :-)



Parens have a higher precedence than 'address of', but the same as '.' and '->'

1: (),[],.,->
2: -,~,!,*,&
3: ++,--,sizeof, casts
4: *,/,%
...


Wrap it! Use &(rgba.r)



[edited by - lessbread on May 24, 2002 4:25:02 AM]
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
foofightr
foofightr
For the record, &rgba.r works fine.

But, function(&rgba); is better like people suggested
LessBread
LessBread
quote:
Original post by foofightr
For the record, &rgba.r works fine.



A C++ compiler probably gets it right, but in my experience when using straight C the parens are needed to get the member address.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Tonic151
Tonic151
Theres a couple different ways to do it.. if you passing the whole structure.. or just 1 piece of the structure...



    
// Structure


typedef struct{
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
} RGBA;

// If you want to pass the whole structure

// I'd do It like this to a function.. And for 1 Element


main()
{
// Just make sure to set a Handle like Below...

RGBA rgba; // Handle to Structure


passFullStruct(&rgba); // Passes the complete structure

pass1Element(rgba.r); // Passing 1 Element of the Struct..

pass1Element(rgba.g); // And So on......

}





[edited by - tonic151 on May 28, 2002 12:11:56 PM]
noober
noober
Dude, I know you probably like to typedef your structures (I do too...) but you shouldn''t. "struct" declarations are helpful in finding out what a variable name is. And its also a good idea to include your tag, cuz i think that code will be buggy or have weird side effect eventually. Just a little advice, take it or leave it i guess.
DrPizza
DrPizza
quote:
Original post by noober
Dude, I know you probably like to typedef your structures (I do too...) but you shouldn''t.

What, so you should suffer the absurd decision to put enums, unions, and structs in one namespace and everything else in another?



Hardly.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
bishop_pass
bishop_pass
Personally, I would have foregone the whole struct and simply used a 4 byte array. The memory layout is the same, but you get the advantage of using arrays in loops if you want, but more importantly, a macro written for a vector or a color will work on the array. Likewise, a function designed to work with a vector can take the color as well.

Then, to pass in the elements one at a time. one simply does something like &rgba[2] or whatever.

As for choosing to pass in the address of the struct or the individual addresses, there are valid reasons for both. The former benefits from efficiency, the latter benefits from making fewer assumptions about data structures used, which is generally a good thing.
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in
invective
invective
you can always union it so you can have both array and member name access.

Topic Locked

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

Sign in to reply to this topic.