| | |
Usage of array of pointers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
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
(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
•
•
Join Date: Mar 2009
Posts: 24
Reputation:
Solved Threads: 9
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.
You can use them e.g. for a table of char-strings:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main(void) { /* Declare the '2D Array' */ char ** ptr = new char * [5]; ptr[0] = new char[20]; ptr[1] = new char[20]; ptr[2] = new char[20]; ptr[3] = new char[20]; ptr[4] = new char[20]; /* Put some data in the array */ ptr[0] = "Hello "; ptr[1] = "wond"; ptr[2] = "er"; ptr[3] = "ful"; ptr[4] = " world !!!"; /* Print the array on the screen */ for(int i = 0; i < 5; i++) cout << ptr[i]; cout << endl; /* Cleanup */ delete[] ptr; /* Wait for the user to press ENTER */ cin.get(); /* Tell the Operating System that everything went well */ return 0; }
Last edited by tux4life; Mar 11th, 2009 at 3:29 pm.
![]() |
Similar Threads
- help with some pointers on Pointers (C++)
- fstream Tutorial (C++)
- A good idea when to use pointers vs "reference variables"? (C++)
- More problems with pointers and functions (C++)
- Trying to use pointers (C++)
- Pointers (C++)
- 'C' initializer problem (C)
- Sorting arrays of pointers with function? (C)
- Pointers (Part II) (C)
- Launching an application from a thread (C++)
Other Threads in the C++ Forum
- Previous Thread: Help with palindromes
- Next Thread: Passing 2D Array of Pointers into a function
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






