Sign in to follow this  
MTclip

Pass mult-Dimen array by Ref

Recommended Posts

Like this do you mean? Or do you mean by pointer?

// arrays as parameters
#include <iostream.h>

void printarray (int arg[], int length) {
for (int n=0; n<length; n++)
cout << arg[n] << " ";
cout << "\n";
}

int main ()
{
int firstarray[] = {5, 10, 15};
int secondarray[] = {2, 4, 6, 8, 10};
printarray (firstarray,3);
printarray (secondarray,5);
return 0;
}

Share this post


Link to post
Share on other sites
Well, as a multidimensional array is simply a pointer-pointer, you could do something like this:


void doSomething(int**& aArray)
{
//...
}

int main(int argc, char* argv[])
{
// initialize our 2D array.
int **aInts = new int*[5];
for(int n=0; n<5; n++)
aInts[n] = new int();


doSomething(aInts);

//...
}

Share this post


Link to post
Share on other sites
pragma Fury I get what you are saying.... but when i do it like that it cannont convert 1 from 'int [20][20]' to 'int **& '


// This is an equivilant do how i call it....

int g_staticPlaceables[TERRAINSIZEX][TERRAINSIZEY];

void loadMap(int**&mapLayer, char* filename)
{
ifstream inFile(filename);
for(int i = 0; i < TERRAINSIZEX; i++)
for(int j = 0; j < TERRAINSIZEY; j++)
inFile >> mapLayer[i][j];
}

main()
{
loadMap(g_staticPlaceables,"staticPlaceables.dat");
}

// OH and how did you create that text box

Share this post


Link to post
Share on other sites
ok nevermind...... im retarded..........
its just

const XVALUE = 10;
const YVALUE = 10;

stupid(int varNAme[XVALUE][YVALUE]){..stuff..};

main()
{
int multiArray[XVALUE][YVALUE];
stupid(multiArray);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by MTclip
// OH and how did you create that text box


Type [ source ] without the space between the word source and the brackets. To close the text box, type [ /source ], again without spaces between /source and the brackets.

If you're curious about other tags, feel free to click the edit button on other people's posts - it'll reveal exactly how it's stored, although you won't be able to actually edit their post.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jingo
Though there will be no difference between that and a non-reference version


Not quite so :)
Consider:

#include <iostream>

void foo(int a[5][5]) { std::cout << a[0][0] << '\n'; }
void bar(int (*a)[5]) { std::cout << a[0][0] << '\n'; }
void baz(int (&a)[5][5]) { std::cout << a[0][0] << '\n'; }


int main()
{
int x[5][5] = {{ 1 }}; // A local 5x5 array
int y[2][5] = {{ 2 }}; // A local 2x5 array
int z[5][2] = {{ 3 }}; // A local 5x2 array
int (*w)[5] = &x[0]; // A pointer to a 5-element array
//int (*v)[5] = &z[0]; // Error; dims don't match

// Pointer to an array of 5 ints

foo(x); // OK
foo(y); // OK
//foo(z); // Error, 2nd dim doesn't match
foo(w); // OK

// Same as above, different syntax

bar(x); // OK
bar(y); // OK
//bar(z); // Error, 2nd dim doesn't match
bar(w); // OK


// Reference to a 5x5 array of int

baz(x); // OK, both dims match exactly
//baz(y); // Error
//baz(z); // Error
//baz(w); // Error
}


Share this post


Link to post
Share on other sites
Quote:
Original post by JingoThough there will be no difference between that and a non-reference version


In addition to Sharlin's point, you cannot pass NULL to a function that expects a reference to a pointer. Whereas if the array was not passed by reference, you could pass a NULL pointer into the function with no problem. ie:


void doSomething(int (&aArray)[5][5])
{
//...
}

void doSomethingElse(int aArray[5][5])
{
//...
}

int main(int argc, char* argv[])
{

int aInts[5][5];


doSomething(aInts); // fine
doSomething(NULL); // error C2664

doSomethingElse(NULL); // fine


//...
}



Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this