home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 1757
Discussions: 188,402, Posts: 2,243,608, Members: 232,631
Old August 15th, 2004, 07:43 PM   Digg it!   #1 (permalink)
Ultimate Member
 
lost-and-found's Avatar
 
Join Date: Oct 2001
Location: Illinois
Posts: 2,959
Send a message via AIM to lost-and-found
c++ pointers

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.
__________________

lost-and-found is offline   Reply With Quote
Old August 16th, 2004, 07:13 AM     #2 (permalink)
may contain mild peril
 
SpookyEddy's Avatar
 
Join Date: Oct 2001
Location: UK
Posts: 3,329
Is there any particular reason you can't just pass by reference?

Regards

ed

SpookyEddy is offline   Reply With Quote
Old 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

Stapler is offline   Reply With Quote
Old 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.
Stapler is offline   Reply With Quote
Old August 23rd, 2004, 07:46 PM     #5 (permalink)
Ultimate Member
 
lost-and-found's Avatar
 
Join Date: Oct 2001
Location: Illinois
Posts: 2,959
Send a message via AIM to lost-and-found
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?
lost-and-found is offline   Reply With Quote
Old 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.
Eric Tetz is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer Kung Foo (c++) SpookyEddy Webmastering and Programming 5 October 24th, 2003 09:20 PM
Can a mouse crash your computer? wju425 General Tech Discussion 5 September 25th, 2003 05:29 PM
C++ pointers Damien019 Webmastering and Programming 5 May 2nd, 2003 12:00 AM
Alternative Mouse Pointer? ClubMed General Tech Discussion 4 August 23rd, 2002 09:43 AM

Most Active Discussions
Is It Just Me? (2906)
Unarmed man on his stomach shot by .. (6)
Misery Loves Company... (2144)
New Build ( Finally ) (7)
CPU wont boot (7)
Building a gaming computer advice (5)
I think I just killed my computer w.. (24)
RCA 52Inch HDTV wont turn on (5)
Folderchat Weekday thread (444)
Recent Discussions
Please help! multiple problems! (4)
How to transfer music from iPod.. (0)
RCA 52Inch HDTV wont turn on (5)
New Build ( Finally ) (7)
Common Spyware Solutions (97)
How do you move a hard-drive to.. (4)
Laptop proccesor to desktop mob.. (1)
What is the best external enclo.. (0)
Partition Magic 7.0 (Unallocate.. (17)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 03:59 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28