• Advertisement

Archived

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

passing an array by pointer or refrence?

This topic is 5499 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
Guest Anonymous Poster
Ooops sorry! My Mistake, bad typo!

Share this post


Link to post
Share on other sites
I''m trying to clear the pass by value or reference question

Here I take an integer ''a'' and pass it to 2 functions. One by value and the other a reference. See how a reference can be used to change the original variable. Value only sends the value, literally; a copy of the variable.

#include <iostream.h>

void pass_by_reference(int *a);
void pass_by_value(int a);

main(void)
{
int a;
cout << "Enter ''a'' value\n";
cin >> a; //gets input from keyboard
cout << "Value entered:" << a << "\n"; // prints the integer

pass_by_value(a); //sends a copy of the value of ''a''
cout << "Pass by value:" << a << "\n"; // after the function exe

pass_by_reference(&a);
//sends the address of ''a'', so a''s value can be changed

cout << "Pass by reference:" << a << "\n";
// prints a''s new value changed by the function i.e. 13 here

return 0;
}

void pass_by_value(int a)
{
a = 13;
// a is local to this function so ''a'' in the main() doesn''t change
}

void pass_by_reference(int *a)
{
*a = 13;
// here even though a is local its reference gives access to the ''a'' in main()
}

Share this post


Link to post
Share on other sites
Actually, your pass by reference function actually passes a pointer
When you pass by value, it passes a copy into the function, a prime example of why you should never pass by value into a function unless you REALLY need to.

Look into copy constructors.

Share this post


Link to post
Share on other sites
passing an array by copy is different to passing a value by copy.

as it turns out, passing an array by copy actually gets converted to passing an array by pointer - it does indeed NOT copy the array. Watch out for that. Look at and compile the following code to show some different strange nomenclature, and ways of manipulating arrays


  
#include <iostream>

using namespace std;

void IntCpyFunc( int in) {in = 10;}
void IntPtrFunc( int* in) {*in = 20;}
void IntRefFunc( int& in) {in = 30;}

// cant have size checking - can pass any size (even though specified as in[ 10])

void ArrayCpyFunc( int in[ 10]) {in[ 0] = 110;}
// following two lines works in EXACTLY the same way as the previous line

//void ArrayCpyFunc( int in[]) {in[ 0] = 110;}

//void ArrayCpyFunc( int* in) {in[ 0] = 110;}


// have to pass in an array of exactly 10 ints

void ArrayRefFunc( int (&in)[ 10]) {in[ 0] = 130;}

// and for fun (by pointer to fixed size array)

void FullyFixedDimension( int in[ 10][ 10])
{
in[ 5][ 8] = 1000;
}

// by pointer to 2 dimensional single dimension fixed array

void PartFixedDimFunc( int in[][ 10])
{
in[ 0][ 0] = 100;
in[ 1][ 0] = 200;
in[ 0][ 1] = 300;
in += 5; // look what dimension this advances on...

in[ 0][ 0] = 400;
}

void main( void)
{
int value = 0;
cout << value << endl;

IntCpyFunc( value);
cout << value << endl;
IntPtrFunc( &value);
cout << value << endl;
IntRefFunc( value);
cout << value << endl;

int valueArray[ 10];
ArrayCpyFunc( valueArray);
cout << valueArray[ 0] << endl;
ArrayRefFunc( valueArray);
cout << valueArray[ 0] << endl;

int bigarray[ 10][ 10];
FullyFixedDimension( bigarray); // valid call


int notsobigarray[ 7][ 10];
//FullyFixedDimension( notsobigarray); // compile time error as array does not match size


PartFixedDimFunc( bigarray); // this works

PartFixedDimFunc( notsobigarray); // and so does this!

cout << notsobigarray[ 0][ 0] << endl;
cout << notsobigarray[ 0][ 1] << endl;
cout << notsobigarray[ 1][ 0] << endl;
cout << notsobigarray[ 5][ 0] << endl;

return 0;
}


[edited by - Shrew on February 3, 2003 8:53:59 AM]

Share this post


Link to post
Share on other sites

  • Advertisement