Quick Question: Pointer to a 2D array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 32
Reputation: coveredinflies is an unknown quantity at this point 
Solved Threads: 0
coveredinflies coveredinflies is offline Offline
Light Poster

Quick Question: Pointer to a 2D array

 
0
  #1
Aug 9th, 2008
Hi, sorry if this has been asked before I did a search but couldn't find it.

How do I define a pointer to a 2D array? (I am trying to pass it into a function).

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,462
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Quick Question: Pointer to a 2D array

 
0
  #2
Aug 9th, 2008
A 2d character array?
void foo(char* ay[][255] )

or like this: void foo(char **ay)
Last edited by Ancient Dragon; Aug 9th, 2008 at 4:31 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Quick Question: Pointer to a 2D array

 
2
  #3
Aug 9th, 2008
Sorry, but char* ay[][255] declares 2D array of pointers to char.
Probably, coveredinflies wants to pass an array as a function argument so right example is most likely
  1. void foo(char ay[][255] );
But it's not the same as
  1. void foo(char **ay);
A pointer to an array is a funny thing in C and C++. Array name is implicitly converted in a pointer in all contexts except argument of sizeof operator. So no need in pointers to arrays.
But we can't pass 2D array declared as a[...][...] in the second foo because of its argument type is a pointer to a pointer to char, but not an implicit pointer to 2D array with 255 columns...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,462
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Quick Question: Pointer to a 2D array

 
0
  #4
Aug 9th, 2008
>>Sorry, but char* ay[][255] declares 2D array of pointers to char.
You are right -- I should not have included the star.

We don't know how the OP declared the 2d array, so it could be either method.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Quick Question: Pointer to a 2D array

 
0
  #5
Aug 9th, 2008
It's impossible to declare 2D array which can be passed as type** if a declarator does not know "how to define a pointer to a 2D array". He/she must declare an array of pointers then fill the array with proper pointers to 1D arrays and so on... It's impossible...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,462
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Quick Question: Pointer to a 2D array

 
0
  #6
Aug 9th, 2008
Impossible? I would call the below a 2d array.
  1. int main()
  2. {
  3. const int rows = 25;
  4. const int columns = 255;
  5. char ** array = new char*[rows];
  6. for(int i = 0; i < rows; i++)
  7. array[i] = new char[columns];
  8. foo(array);
  9. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Quick Question: Pointer to a 2D array

 
0
  #7
Aug 9th, 2008
It seems you don't understand my previous post.
I never said that it's impossible for you (and me)...
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Quick Question: Pointer to a 2D array

 
0
  #8
Aug 9th, 2008
In the true sense of the word(s) "2D array" a construct with array of pointers is not C/C++ 2D array.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 32
Reputation: coveredinflies is an unknown quantity at this point 
Solved Threads: 0
coveredinflies coveredinflies is offline Offline
Light Poster

Re: Quick Question: Pointer to a 2D array

 
0
  #9
Aug 10th, 2008
Originally Posted by ArkM View Post
  1. void foo(char ay[][255] );
Thanks this is what I needed. Pretty obvious in hindsight. Sorry for any ambiguity in original post.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Quick Question: Pointer to a 2D array

 
0
  #10
Aug 10th, 2008
It was Ancient Dragon's post (I have corrected a misprint only).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC