Jump to content
  • Advertisement

Archived

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

Nothingness

passing an array by pointer or refrence?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to have a function that passes a char array filled with "." All I want to do is have another function output the array. With what I did, it makes the first line dot filled (what I want), but then the other lines depreciate into gibberish. Once I figure out how to correctly pass an array I have other functions that wants to change only certain elements in the array. So how would I go at that? What I think is just make the whole matrix "." again, then set one certain element to the letter I want. Is that right? Also if you could can you explain how would I pass a multidemintional(sp) array. As you can see, I am still trying to figure out pointers and refrences.

Share this post


Link to post
Share on other sites
Advertisement

  
void Pass1DArray(char *arr); // these two functions take an array as an argument - they are pretty much the same

void Pass1DArray(char arr[]);

char a[] = "hello";

// now to pass the array, you would send "a" which is really just a pointer to a[0]

Pass1DArray(a);


// a function that take a character as an argument

void PassChar(char c);

// now you can pass a char into the function for your array

PassChar(a[3]); // passing the character ''l''


// NOTE: the above functions taking arrays don''t take the size. you should always pass the size of the array also so that you don''t write to different memory locations



// an array that takes a multidimensional array

void Pass2DArrayPtr(char **arr, int rows, int cols); // i''m not sure if this is correct

void Pass2DArray(char arr[][numOfCols], int rows); // this works


// if i missed anything it''s because i don''t fully understand your question so elaborate on any unanswered questions

Share this post


Link to post
Share on other sites
So what is the difference between passing a variable by reference and byvalue?

What is the difference between the following functions:?


  
void functionval(int *value);
void functionref(int &value);


Tazzel3d ~ Dwiel

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Always send a pointer if you''re sending an array into a function, and do the de-referencing in the code.

Multidimensional pointers are exactly the same as singular dimensional arrays, just you access them.

say for example:


  
void foo( char* whatever )
{
for(int a = 0; a < 10; a++)
cout << whatever[a];
}

void foo2( char* whatever )
{
for(int a = 0; a < 10; a++)
for(int b = 0; b < 10; b++)
cout << whatever[a][b];
}

int main(void)
{
char text[10];
char multidim[10][10];

foo(text);
foo2(multidim);

return 0;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Tazzel3D
So what is the difference between passing a variable by reference and byvalue?

What is the difference between the following functions:?



    
void functionval(int *value);
void functionref(int &value);


Tazzel3d ~ Dwiel
well, the first one actually is being sent as a pointer and not by value, the second one is a reference, like a pointer only all the dereferencing etc... is hidden from you.

Share this post


Link to post
Share on other sites
The One dimensional array worked, but the 2 dimensional dosen''t. Im getting:
"error C2109: subscript requires array or pointer type"
"''ShowTank2'' : cannot convert parameter 1 from ''char [5][5]'' to ''char *
''Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast"

And my ShowTank2 function does have a pointer parameter. So again, how do I pass a multidimensional array?

Share this post


Link to post
Share on other sites
I've done it before I think, but it gets kind of weird. You might want to try dereferencing? twice; so something like: int ** array[5][5]. I have no idea if that will work though..

Another possibility (unless you must have a multidimensional array) is to put it into one array in linear fashion. Then to access it, you would use array[x+y*maxLength]. So to output, it might look like:


    

char array[5*5];

for (int j = 0; j < 5; j++)
{
for (int i = 0; i < 5; i++)
{
cout<<array[i+j*5]
}
}
]


Or something like that; I've used it before. You would then only have to pass a one dimension array to your function. I think it works because of the way that arrays are stored in memory... or something like that

Peon

[edited by - Peon on February 2, 2003 1:54:25 PM]

Share this post


Link to post
Share on other sites
In the AP''s example, foo2 should be
void foo2 (char** whatever) {
// blah blah
}

This is because a multidimensional array is essentially an array of arrays

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!