#### Archived

This topic is now archived and is closed to further replies.

# easy question about passing by reference

This topic is 5622 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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!

##### Share on other sites
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

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

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

##### Share on other sites
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

##### Share on other sites
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!

##### Share on other sites
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

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!

##### Share on other sites
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 )

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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]

##### Share on other sites
*mental note. Never piss off Stoffel

##### Share on other sites
quote:
Original post by Stoffel
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]

1.**a method doesn t work int *a[] is not a 2d array
2.stoffel you are a moderator a person to respect but your tone demands the opposit

[edited by - Basiror on October 1, 2002 12:09:08 PM]

##### Share on other sites
quote:
Original post by Basiror
2.stoffel you are a moderator a person to respect but your tone demands the opposit

This point seems to come up often. I''ve modified my profile (job and description, specifically) to provide you more information.

##### Share on other sites
ooo don''t start a war on my part. I got it working now thanks to the first guy

Planetblaze.com - www.planetblaze.com - As METAL as it gets!

##### Share on other sites
Personally, Stoffel, you sound like a large-type asshole who has nothing better to do than to make people feel bad because they dont know something.

If this is how you act at the company you work at, then I don''t know who would want to work for you.

##### Share on other sites
quote:
Original post by Stoffel
[quote]Original post by Basiror
2.stoffel you are a moderator a person to respect but your tone demands the opposit

This point seems to come up often. I''ve modified my profile (job and description, specifically) to provide you more information.

+
It doesn''t mean I''m nice to posters--especially stupid posters.
+

hm i wonder what you consider to be a stupid poster

people who know less than you but know more than you did at their age

or newbies who don t know what they are doing?

nobody can be more stupid than someone who acts aggressive without any real reason

##### Share on other sites
quote:
people who know less than you but know more than you did at their age

or newbies who don t know what they are doing?

It has nothing to do with that.

Stoffel, is saying that those that ''do not know the answer'' should not clutter up a thread with inane comments. And instead those that don’t know the answer either should watch the thread till a proper answer is finally provided.

By popping off wrong answers generates more confusion to the posters woes.

If you felt Stoffel''s tone was too much then you take criticism poorly and should ponder whether programming industry is really your cup of tea. This goes especially for those that answered the question wrong.

##### Share on other sites
Allways the pointers seems to be the hardest thing in C/C++

* Do not do an int a[800][600]. Do insteadd a int *a = new int[800*600] (or a malloc in C). It save you from many troubles (but take care of delete/free ).
* Do the function as f(int xSize, int YSize, int* pData). It''s more easy to understand (on a long term).

* If you want an int a[800][600]; fine. Just remember that''s just some data in memory and so you can access it as raw data:
pass it to f( int xSize, int YSize, int* a) as f( 800,600, &a[0][0]).

Better - you are in C++ - so on a long term it''s better to use a class to store your array as it probablly represents something (the screen ) and then make it a singleton if you want (safer), or construct them with new and pass them as objects to functions or better use member functions to do the job on the array....so on... so on...
Good luck,
Pet.

##### Share on other sites
Blimey!
All I did was look up passing multi-dimensional arrays to functions in Schildt''s "C++: The Complete Reference", and pass on the info I found to the original poster, who seemed happy enough with it. Where did the rest of this crap spring from?????

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