August 15th, 2004, 07:43 PM
|
#1 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Illinois
Posts: 2,959
|
Hey, let's say I have a bit of code that goes like: Code: something(int*);
void main(){
int array[2];
something(array);
}
void something(int *arraypointer){
//do things here....
} This program would use a pointer to manipulate a 1-d array created in main(). How would a syntax look for a 2-d array? If I wanna use pointers to manipulate an array such as: "int Array[2][2];" how would I pass it as a pointer. |
| |
August 16th, 2004, 07:13 AM
|
#2 (permalink)
| | may contain mild peril
Join Date: Oct 2001 Location: UK
Posts: 3,329
|
Is there any particular reason you can't just pass by reference?
Regards
ed |
| |
August 20th, 2004, 09:50 AM
|
#3 (permalink)
| | Junior Member
Join Date: Aug 2004 Location: Australia
Posts: 2
|
I think I know where your coming from,
not that I can help much,
I'm currently losing sleep trying to pass a 2-d array to a function;
void copyArray(int noOfOptions, char** options)
{
char choice[9][81];
for(int i = 0; i < noOptions; i++)
strcpy(choice[i],options[i]);
} // or similar
I've been given this function as correct but I just can't work out the syntax to pass a 2-d array.
It seems to be quite different to the simple 1-d array example you have shown.
The only thing I've been able to pass it is a
char** // A pointer to a pointer?
I think I need to revise array memory management  |
| |
August 20th, 2004, 10:34 AM
|
#4 (permalink)
| | Junior Member
Join Date: Aug 2004 Location: Australia
Posts: 2
|
I love these forums, thanks lost-and-found, once again talking abouts it has somehow helped!
This is what I've got... char* options[9] = {"1.Blah" , "2.." , "3.." , ... };
then i can call copyArray(9, options);
to copyArray(int noOfOptions, char** options)
{
// copy 2-d array
}
and it is passed and processed as a 2-d array! At last!
This is the only way I have been able to pass it to the code above that uses pointers. Hope it helps.
If anyone can clarify why you would want to do it this way I'd love to listen. |
| |
August 23rd, 2004, 07:46 PM
|
#5 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Illinois
Posts: 2,959
|
Hey guys, sorry to bump this thread so late, but I figured it out and just wanna share the solution for anyone itnerested.
THe purpose: I was writing a matrix multiplicator and needed several functions where the user enters data into the matrices that were created in main(). so I had functions called DataA() and DataB() because I had two matrices, then at any point I needed the user to re-enter or modify the data in any of the matrices I just sent them to the correct function. Here's the source for the program...which I release under GPL http://www.csupomona.edu/~wtruty/images/Matrix.cpp
Now, here's what I did.
Let's say I have this code: Code: void DataA(int[2][2]); //function prototype
void main(){
int MatrixA[2][2];
DataA(MatrixA); //pass MatrixA[2][2] to DataA()
}
void DataA(int Matrix[2][2]){
Matrix[0][0]+=2;
} It turns out when passing the 2-d array like that, it's like passing a pointer to that array, and even though I'm adding 2 to row0col0 in Matrix[2][2], it will actually also add 2 to row0col0 of MatrixA......understand? |
| |
August 23rd, 2004, 09:21 PM
|
#6 (permalink)
| | Junior Member
Join Date: Aug 2004
Posts: 2
| Quote: |
Originally Posted by lost-and-found Now, here's what I did.
Let's say I have this code: Code: void DataA(int[2][2]); //function prototype
void main(){
int MatrixA[2][2];
DataA(MatrixA); //pass MatrixA[2][2] to DataA()
}
void DataA(int Matrix[2][2]){
Matrix[0][0]+=2;
} It turns out when passing the 2-d array like that, it's like passing a pointer to that array, and even though I'm adding 2 to row0col0 in Matrix[2][2], it will actually also add 2 to row0col0 of MatrixA......understand? | Yes, arrays are always passed by address. You also could have written: Code: void DataA (int Matrix[][2]);
int main() {
int MatrixA[2][2];
DataA(MatrixA); // pass MatrixA to DataA()
return 0;
}
void DataA (int Matrix[][2]) {
Matrix[0][0] += 2;
} C(++) doesn't require you to specify the first array dimension, it doesn't need it for it's index calculations. |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |