easy question about passing by reference

Started by
16 comments, last by Real World 21 years, 6 months ago
Must have done this a million times but I cant remember how to now or I have something wrong somewhere else... I have a 2D array (c++) called array[800][600] which I want to pass as a pointer to another function. How do I a) call the function say if the function is called function() b) define the function in the header library and c) reference the first and second parts of the array in the function Hope that makes sense. Cheers Planetblaze.com - www.planetblaze.com - As METAL as it gets!
Advertisement
DISCLAIMER: Please note I am no expert but the following is as much as I can remember, and I hope it helps.

You pass multidimensional arrays by reference, and in all but the leftmost dimension you must specify a size.

So in the header:void function(int a[][600]);orvoid function(int a[800][600]);will work.  

To call:
function(array); // note no & is required as arrays are automatically passed by ref

Not sure about c, soz.

pan narrans
Study + Hard Work + Loud Profanity = Good Code

[edited by - pan narrans on September 30, 2002 2:39:44 PM]
Minister of Propaganda : leighstringer.com : Nobody likes the man who brings bad news - Sophocles (496 BC - 406 BC), Antigone
function( type **array);
function( type *array[]);
both is possible please don t ask me now how to do it with an 3d array i don t know it and i don t think it is possible though

please correct me when i am wrong
but the function parameters are quite common you can find them in nearly every main function of console applications
http://www.8ung.at/basiror/theironcross.html
quote:Original post by Basiror
function( type **array);
function( type *array[]);
both is possible please don t ask me now how to do it with an 3d array i don t know it and i don t think it is possible though

please correct me when i am wrong
but the function parameters are quite common you can find them in nearly every main function of console applications


error C2664: ''CheckForCollisionWithPlayer'' : cannot convert parameter 5 from ''unsigned long [800][600]'' to ''unsigned long ** ''

The first one doesnt seem to work either. it compiles but the array doesnt seem to be passed.

Planetblaze.com - www.planetblaze.com - As METAL as it gets!
quote:Original post by pan narrans
DISCLAIMER: Please note I am no expert but the following is as much as I can remember, and I hope it helps.

You pass multidimensional arrays by reference, and in all but the leftmost dimension you must specify a size.

So in the header:void function(int a[][600]);orvoid function(int a[800][600]);will work.   

To call:
function(array); // note no & is required as arrays are automatically passed by ref

Not sure about c, soz.

pan narrans
Study + Hard Work + Loud Profanity = Good Code

[edited by - pan narrans on September 30, 2002 2:39:44 PM]


the [][600] method seems to work cheers

Planetblaze.com - www.planetblaze.com - As METAL as it gets!
allocating an array like this array[100][100] isn''t bad, but internally the compiler uses this kind of math to access the element : index=i*100+j , this is how a compiler translates the
a[j] access , so i think that *array = new int [100*100] ,is better , just because you can write an optimized accessing function and do all sort of pointer arithmetic trick like shifting by a power of 2 when reading/writing
i hope it helps, sorry for my english ( this will be my new signature )


quote:Original post by Basiror
function( type **array);
function( type *array[]);

This won''t work. type **array is a different type than type var[600][800].

pan narrans'' response was 100% correct, except for the fact that his suggestion _was_ C, so it was even more correct than he thought.

Please ignore other insane responses.
if you want to use void function(type **a) the array should be allocated dynamically that is declare the array as:

type **array = new type[800][600]

or whatever dimensions you want. then you can just pass in array as the parameter for the function.
OK, maybe it's just the latent road rage since I sat gridlocked at a light through 3 cycles before getting home, but you people are freakin' morons.

  int **array = new int[800][600];   

error C2440: 'initializing' : cannot convert from 'int (*)[600]' to 'int ** '

You know, a sign of real intellect is keeping your mouth shut when you don't know what you're talking about.

Again, the very first freakin' response was the absolute, 100% correct answer. The rest of you need to take a class and shut your pie-holes.

[edited by - Stoffel on September 30, 2002 10:00:33 PM]
*mental note. Never piss off Stoffel

This topic is closed to new replies.

Advertisement