Skip to main content
gamedev.net
🔒 Locked

Calling by reference.

Started by witchdrash Mar 14, 2003 at 11:40 AM 4 replies 250+ views
Original Post
witchdrash
witchdrash
I asked this question a few days ago and realised that the suggestion I used was copying the variable, which means, of course that the values the procedure creates do not return to the previous procedure so here it is again. I have 2 procedures: void s_loopback_init(TRACK_OBJ *toreceive_trackobject) { } and: void generate_track(TRACK_SECTION *toreceive_trackdata[],int driftX[], int driftY[], int trackSize) { } s_loopback_init calls generate_track using the code: generate_track(toreceive_trackobject->track,toreceive_trackobject->driftX,toreceive_trackobject->driftY, toreceive_trackobject->size); Now I need to pass the address of track (from toreceive_trackobject->track) to the next one, rather than copy it because I need that data filled, so what I need to do is the equivilant of: toreceive_trackobject->&track so that the address is passed. Are there any suggestions on how to do this? WDA
stu_pb
stu_pb
Try
&(toreceive_trackobject->track )

I should probably be working now...
I should probably be working now...
witchdrash
witchdrash
No still get the error:
C2664 : cannot convert parameter 1 from TRACK_SECTION(*)[1200] to TRACK_SECTION *[]

WDA

[edited by - witchdrash on March 14, 2003 1:22:40 PM]
ToohrVyk
ToohrVyk
I, personnaly, would have written :


void generate_track(TRACK_SECTION ** toreceive_trackdata,int * driftX, int * driftY, int trackSize)
{

}

generate_track(&(toreceive_trackobject->track),
&(toreceive_trackobject->driftX),
&(toreceive_trackobject->driftY),
toreceive_trackobject->size);



with :


struct TRACK_OBJECT {
TRACK_SECTION track[1200];
int driftX[1200];
int driftY[1200];
int size;
};




EDIT - breaking my code lines... sorry but it fits MY screen :-p

ToohrVyk
-------------
Extatica - a free 3d game engine
Available soon!
Click here to learn more

[edited by - ToohrVyk on March 14, 2003 1:44:46 PM]
Fruny
Fruny
If you're using C, you can't do pass by reference, period. You have to make do with passing a pointer to the data.

The rest of the answer will therefore assume you are using C++.

If you have a type int then int* indicates a pointer to int, and int& indicates a reference to int.

Thus if you have a function func( int x ) which passes by value, func( int& x ) will pass by reference.


void s_loopback_init(TRACK_OBJ *& toreceive_trackobject)
{
}

void generate_track(TRACK_SECTION *& toreceive_trackdata[],
int driftX[], int driftY[], int trackSize)
{
}


This does not require any special change at the call point - advantage which references have above manually passing a pointer to the data. const references also let you pass literals, the address of which cannot be taken ( &1 is an error ).

Of course, passing a pointer by reference only matter if you want to modify the pointer itself . Unless declared const, the data can always be modified. And as it happens, you are trying to pass an array (or so it seems, TRACK_SECTION *[] is an array of pointers). Given that array addresses can never be modified ( you can do array[offset] = value; but not array = address ), this is more or less pointless (not quite, there is a reason why you would want to use pass by reference with an array, but it is completely irrelevant here since you're not using function templates.)

Learn to use fewer pointers and arrays and more references, C++ strings and containers.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]



[edited by - Fruny on March 14, 2003 1:46:30 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Fruny
Fruny
ToohrVyk, for Eris'' sake, break your lines when you''re using code [code] blocks.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

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

Sign in to reply to this topic.