English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am writeing a program in c++.and I need to make an array
of pointers but I dont know whether it is possible or not.if you think
your answer is right and you can help me please put your email
in your answer becouse I might need to get more help.
thanks

2006-09-14 08:24:01 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

4 answers

Yes, you absolutely can make an array of pointers. Here are a couple of methods you can use.....

Statically Allocated Version
If you know how many pointers you will have in your program you can use a #define or const variable for the definition such as:

#define MAX_POINTERS 10

int* i_pArray[MAX_POINTERS];

(Don't forget to initialize the array to NULL when your program starts)

memset(&i_pArray, NULL, sizeof(int)*MAX_POINTERS);

Dynamically Allocated Version
If you dont know how many pointers or objects you will need until the program is running you can create the array dynamically

int iMaxPointers = 10;

int* i_pArray = (int*)malloc(iMaxPointers * sizeof(int));

memset(&i_pArray, NULL, sizeof(int)*iMaxPointers);

Stanard Template Library (STL) Version
Some find STL confusing at first but once they start using it they love it. STL has linked lists, maps, and for this example vectors...very easy to use.

#include
using namespace std;

// declaration of your array. will grow as needed
vector i_pArray;

// create one pointer when adding to the array
int* pValue = new int[];

// add pointer to bottom of array
i_pArray.push_back(pValue);

At this point you can index the array just like normal....

int* pValue = i_pArray[10]; // returns 10th element

You could get the size of the array.....

int iCount = i_pArray.size();


Hope this information is helpful.

2006-09-14 18:05:21 · answer #1 · answered by Anonymous · 0 0

char* pointers[4]; // declare an array of 4 pointers

int n=600, o=500, p=400, q=300; // declare some integers (could be any type)

// Set the pointers in your array to point to the address of each integer
pointers[0] = &n;
pointers[1] = &o;
pointers[2] = &p;
pointers[3] = &q;
// the array of pointers now contains four pointers that point to four integers set to four different values

// Output the value of the pointer
int cur_val=0; // will equal value of the current pointer in each iteration of loop
for (int t=0; t<4; t++ )
{
cur_val = *pointers[t]; // dereference pointer at current index, set cur_val to equal value stored there
cout<<"pointer["< }


Good luck

2006-09-14 15:55:11 · answer #2 · answered by guitaryogi 2 · 0 0

Yes, you can make an array of pointers in C++. Search in Internet to find more about how to use array of pointers in C++.

Best of luck.

thanks

2006-09-14 15:35:09 · answer #3 · answered by yaramala 2 · 0 0

its possible, just like you can make an array of string:

char *myArray[ ];

(don't quote me on the syntax, its been a while since I have done it, but I know it can be done).

2006-09-14 15:28:25 · answer #4 · answered by John J 6 · 0 0

fedest.com, questions and answers