Usage of array of pointers

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

Join Date: Dec 2008
Posts: 5
Reputation: idb_study is an unknown quantity at this point 
Solved Threads: 0
idb_study idb_study is offline Offline
Newbie Poster

Re: Passing 2D Array of Pointers into a function

 
0
  #1
Mar 10th, 2009
Hi All,

Could you kindly help me regarding the usage of array of pointer (1dimension or 2), I mean where we can use it.

Regards,
IDB
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Usage of array of pointers

 
0
  #2
Mar 10th, 2009
Array of Pointers can be used to do fast sorting.
For eg. If you want to sort a unsorted array of integer. You generally can use all sorts of algorithms on the int array. But it would be less efficient as the runtime head for moving the values of int array will be more.
Now other alternative is to sort by using array of pointers. Say, now, you create array to int pointers (int*) and let each one of them point to the corresponding elements of the integer array. Now when you need to sort them, just re-locate the pointers in correct order, which would be faster.
Read: http://www.gidforums.com/t-10801.html
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 24
Reputation: boblied is an unknown quantity at this point 
Solved Threads: 9
boblied boblied is offline Offline
Newbie Poster

Re: Usage of array of pointers

 
0
  #3
Mar 10th, 2009
Another use is to hold a collection of heterogeneous objects. For instance, if you have the classic example of things derived from Shape (triangle, circle, etc). If you make an array of Shape objects, then they are just base objects (not triangle etc). But if you make it an array of pointers to Shape, then you can access the derived classes polymorphically.
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: Usage of array of pointers

 
0
  #4
Mar 10th, 2009
you can use them to dynamically initialize memory (mean that at runtime u decide the number of elements of the array)
for both 2d And 1d
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: Usage of array of pointers

 
0
  #5
Mar 11th, 2009
You can use them e.g. for a table of char-strings:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. /* Declare the '2D Array' */
  8. char ** ptr = new char * [5];
  9. ptr[0] = new char[20];
  10. ptr[1] = new char[20];
  11. ptr[2] = new char[20];
  12. ptr[3] = new char[20];
  13. ptr[4] = new char[20];
  14.  
  15. /* Put some data in the array */
  16. ptr[0] = "Hello ";
  17. ptr[1] = "wond";
  18. ptr[2] = "er";
  19. ptr[3] = "ful";
  20. ptr[4] = " world !!!";
  21.  
  22. /* Print the array on the screen */
  23. for(int i = 0; i < 5; i++)
  24. cout << ptr[i];
  25.  
  26. cout << endl;
  27.  
  28.  
  29. /* Cleanup */
  30. delete[] ptr;
  31.  
  32. /* Wait for the user to press ENTER */
  33. cin.get();
  34.  
  35. /* Tell the Operating System that everything went well */
  36. return 0;
  37. }
Last edited by tux4life; Mar 11th, 2009 at 3:29 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