PDA

View Full Version : [SOLVED] pointer arrays?



Zeotronic
June 30th, 2008, 07:20 PM
C++: To my knowledge I can make a pointer array with 'new' in this fashon:

int* value=new int(5);
But I'm curious what the right method is to make a pointer array, which points to values which already exist? You see, I'm afraid if I do it wrong I'll have problems I cant explain.

StOoZ
June 30th, 2008, 07:30 PM
what you did is not an array, its just a dynamically allocated pointer which points to an int and initialized with 5.

dwhitney67
June 30th, 2008, 07:39 PM
This is the correct syntax used to allocate an array from the heap:

int *array = new int[5];
Perhaps you had a typo?

If you want to declare an array on the stack:

int array[5];

As for your question, I am not sure what you are looking for; perhaps something like the following?


int value1 = 10;
int value2 = 20;
int value3 = 30;
int value4 = 40;
int value5 = 50;

int *array[5];

array[0] = &value1;
array[1] = &value2;
array[2] = &value3;
array[3] = &value4;
array[4] = &value5;

WW
June 30th, 2008, 07:52 PM
Check out the wikipedia article about new (http://en.wikipedia.org/wiki/New_(C%2B%2B)). To create an array of five pointers to integers:


int** p_parray = new int* [5];

Also, from the article,


Initializers cannot be specified for arrays created with new. All elements of an array are initialized with the default constructor of the type. If the type does not have a default constructor, this is a compile-time error.


And, for kicks:

newtest.cpp


#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
// Create a new integer with the value 5
int* p_scalar = new int(5);

// Create an array of integers
int* p_array = new int[5];

// Create an array of pointers to integers
int** p_parray = new int* [5];

for (int i = 0; i < 5; ++i)
{
p_array[i] = *p_scalar + i;
p_parray[i] = p_array+i;
}

for (int i = 0; i < 5; ++i)
{
cout << "i=" << i << " " << *(p_parray[i]) << endl;
}

return 0;
}

Compile and run:


$ g++ -Wall newtest.cpp -o newtest
$ ./newtest
i=0 5
i=1 6
i=2 7
i=3 8
i=4 9
$

Can+~
June 30th, 2008, 09:48 PM
But I'm curious what the right method is to make a pointer array, which points to values which already exist? You see, I'm afraid if I do it wrong I'll have problems I cant explain.

Arrays are just blocks of memory on juxtaposition which you can iterate with pointer arithmetic *(array+sizeof(type)*n) or using it's equivalent wrapper array[n]. So making a "pointer array" is pretty much any method suggested by dwhitney67 (unless you mean making an array of pointers) .

And what do you mean with "which points to values which already exist"?

Zeotronic
June 30th, 2008, 10:12 PM
Perhaps you had a typo?
Yea, it was a typo. I wonder how I messed that up? :confused:

perhaps something like the following?
That was the only thing I could think of... I just wanted to make sure that was the best way to do it... or even a viable way to do it.

And what do you mean with "which points to values which already exist"?
dwhitney67's last example is pretty well exactly what I mean... only instead of being ints... mine are already new'd int pointers to single values. (in actuality mine arn't ints at all, but its close enough for comparison)