passing an array by pointer or refrence?
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.
void Pass1DArray(char *arr); // these two functions take an array as an argument - they are pretty much the samevoid 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 argumentvoid PassChar(char c);// now you can pass a char into the function for your arrayPassChar(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 arrayvoid Pass2DArrayPtr(char **arr, int rows, int cols); // i''m not sure if this is correctvoid 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
So what is the difference between passing a variable by reference and byvalue?
What is the difference between the following functions:?
Tazzel3d ~ Dwiel
What is the difference between the following functions:?
void functionval(int *value);void functionref(int &value);
Tazzel3d ~ Dwiel
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:
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;}
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.
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?
"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?
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:
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]
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]
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
void foo2 (char** whatever) {
// blah blah
}
This is because a multidimensional array is essentially an array of arrays
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement