RA>I was wondering what is the right way to pass a structure to a
RA>subroutine. I fiddled and fiddled and the below example works, but
RA>it looks, well redundant.
RA>
RA>
RA>THE STRUCTURE
RA>struct Thing You = {1,1,'\@'};
RA> void ShowThing(Thing *T) // Return unnecessary so use void
RA> { // Also, in C++ struct not required
RA> gotoxy(T->Across, T->Down); // Use -> operator to dereference
RA> pointer putch(T->ThingChar);
RA> }
Well, you could always pass by reference. Instead of void
ShowThing(Thing *T) use void ShowThing(Thing & T) then the code would be
gotoxy(T.Across, T.Down);
pointer putch(T.ThingChar);
Passing by reference is only in C++, not C. It does the same thing, just
in a different form. If you're not used to pointers, it might be easier.
RA>Thank you very much. I'm going to work with this. Any other people I
RA>talk to about C, tell me not to pass structures, just the fields from
RA>the struct. Seemed like a waste not to pass the whole structure.
It's your program, do what you like. Actually, you don't _really_ pass
the whole structure. You pass the address of it then dereference it,
hence the need for the ->'s instead of .'s. I think maybe C++ can pass
the whole structure and not just the address of it. I'm not 100% sure on
this point though.
Hope this helps a bit.
Danial.
___
X SLMR 2.1a X
--- Maximus/2 3.00
---------------
* Origin: Hunter Connection OS/2 BBS 24hrs (049) 57-1801 (3:622/403)
|