Passing 2D Array of Pointers into a function

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

Join Date: Sep 2005
Posts: 5
Reputation: NagendraR is an unknown quantity at this point 
Solved Threads: 0
NagendraR NagendraR is offline Offline
Newbie Poster

Passing 2D Array of Pointers into a function

 
0
  #1
Feb 23rd, 2006
Dear All,
I am facing a problem in passing 2D array of pointers to a function.

can any one give some suggestions on this.

1. How the Declaration fo the function should be ?
2. How to pass 2D array of pointers to that function?
3. How to access the array elements.

The aray declaration is *ptr[2][3] = { {"AA","XX","FF"},
{"DD","QQ","EE"},
};

It would be great helpful for me,since I am struggling with this one
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Passing 2D Array of Pointers into a function

 
0
  #2
Feb 23rd, 2006
If you're struggling, duplicate your declaration:
  1. void foo ( int *arg[2][3] );
  2.  
  3. int main ( void )
  4. {
  5. int *ptr[2][3];
  6.  
  7. foo ( ptr );
  8.  
  9. return 0;
  10. }
Within foo, you use arg just as you would use ptr unless you're trying to use sizeof, in which case you probably won't get the result you want.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 5
Reputation: NagendraR is an unknown quantity at this point 
Solved Threads: 0
NagendraR NagendraR is offline Offline
Newbie Poster

Re: Passing 2D Array of Pointers into a function

 
0
  #3
Feb 24th, 2006
Originally Posted by Narue
If you're struggling, duplicate your declaration:
  1. void foo ( int *arg[2][3] );
  2.  
  3. int main ( void )
  4. {
  5. int *ptr[2][3];
  6.  
  7. foo ( ptr );
  8.  
  9. return 0;
  10. }
Within foo, you use arg just as you would use ptr unless you're trying to use sizeof, in which case you probably won't get the result you want.

Hi,
Thanks for your reply.

But I have diffrent types of 2D array of pointers which varies rows and columns size, So in the function declaration/definition I can not specify the rows and columns size.

So I am trying for an alternative.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Passing 2D Array of Pointers into a function

 
0
  #4
Feb 24th, 2006
>So in the function declaration/definition I can not specify the rows and columns size.
Then you can't use an array. The size of the first dimension may be omitted, but the others are required, and the size of an array dimension must be a compile-time constant. If you want to pass a two dimensional array of any size to a function, you use dynamic memory:
  1. #include <stdlib.h>
  2.  
  3. void foo ( int ***arg );
  4.  
  5. int main ( void )
  6. {
  7. int ***ptr = malloc ( 2 * sizeof *ptr );
  8. int i;
  9.  
  10. for ( i = 0; i < 2; i++ )
  11. ptr[i] = malloc ( 3 * sizeof *ptr[i] );
  12.  
  13. foo ( ptr );
  14.  
  15. for ( i = 0; i < 2; i++ )
  16. free ( ptr[i] );
  17. free ( ptr );
  18.  
  19. return 0;
  20. }
You have more options in C++, but you neglected to mention what language you're using.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 53
Reputation: arshad115 is an unknown quantity at this point 
Solved Threads: 2
arshad115's Avatar
arshad115 arshad115 is offline Offline
Junior Poster in Training

Re: Passing 2D Array of Pointers into a function

 
0
  #5
Mar 10th, 2009
here is how you initialize the function and pass the values
  1.  
  2. char *function(char **array)
  3. {
  4. //your code here
  5. return 0;
  6. }
  7.  
  8. int main()
  9. {
  10.  
  11. //put values in array
  12.  
  13. function(p); //passing to function
  14.  
  15. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Passing 2D Array of Pointers into a function

 
0
  #6
Mar 11th, 2009
Maybe you want to take a look at my code:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void func(char **& ref_to_ptr); /* Function declaration */
  6.  
  7. int main(void)
  8. {
  9. /* Declare the '2D Array' */
  10. char ** ptr = new char * [5];
  11. ptr[3] = new char[20];
  12.  
  13. /* Put some data in the array */
  14. ptr[3] = "k";
  15.  
  16. /* Print the first value on the screen */
  17. cout << "First value: " << ptr[3] << endl;
  18.  
  19. /* Pass the array by reference to the function 'func()' */
  20. func(ptr);
  21.  
  22. /* Again we print on the screen what's in the '2D Array' */
  23. cout << "Second value: " << ptr[3] << endl;
  24.  
  25. /* Wait for the user to press ENTER */
  26. cin.get();
  27.  
  28. /* Cleanup */
  29. delete[] ptr;
  30.  
  31. /* Tell the Operating System that everything went well */
  32. return 0;
  33. }
  34.  
  35. void func(char **& ref_to_ptr)
  36. {
  37. /* This function demonstrates how to change the value of it's argument(s) */
  38. ref_to_ptr[3] = "tux4life";
  39. }
Last edited by tux4life; Mar 11th, 2009 at 3:40 pm.
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