7

I forgot how to initialize the array of pointers in C++ like the following:

int * array[10];

Is this a proper solution like this? Here:

array = new int[10];

 // Is this the correct way?
14
  • What if the pointer is an object of another structure - ie. user defined structure... I'm guessing it's the same way?? @sorosh_sabz can you give an example? Commented Aug 12, 2016 at 14:00
  • 1
    int * array[10] = {}; That initializes the pointers to nullptr. Commented Aug 12, 2016 at 14:00
  • 1
    "how to initialize the array of pointers" That rather depends on what you want to initialize it to. What's the goal of the exercise? Commented Aug 12, 2016 at 14:01
  • 1
    You might want int * array[10] = {new int, new int, ...}; ? Commented Aug 12, 2016 at 14:02
  • 1
    @Jean-FrançoisFabre It's aggregate initialization, from C++98 (?) Commented Aug 12, 2016 at 14:14

6 Answers 6

14
int * array[10];

defines 10 pointers on 10 int arrays statically

To go dynamic:

int **array = new int *[10];

Better solution since you use C++: use std::vector

std::vector<int *> v;
v.resize(10);
v[2] = new int[50];  // allocate one array

Since we're using vectors for the array of pointers, lets get rid of the pointers completelely

std::vector<std::vector<int> > v;
v.resize(10);
v[2].resize(50);  // allocate one array

Then access the array like a matrix:

v[3][40] = 14;

Going further, one way to initialize all the rows, using C++11, making a 10x50 int matrix in the end (but size can also change within the loop if we want). Needs gcc 4.9 and g++ -std=c++11 to build

std::vector<std::vector<int> > v;
v.resize(10);
for (auto &it : v)
{
   it.resize(50);  // allocate arrays of 50 ints
}
Sign up to request clarification or add additional context in comments.

4 Comments

This pretty much covers the whole thing that I needed! Thanks!
Even better, use std::array for the static part, e.g. std::array<std::vector<int>, 10>.
Didn't know that. Nice one. But the OP wanted it to be dynamic. I'll keep that in mind.
I must've clicked on it accidentally, it's there now :)
6

In general in most cases there is no great sense to initialize the array with exact addresses. You could assign the addresses or allocate appropriate memory during the usage of the array.

Usually there is sense to initialize an array of pointers with null pointers. For example

int * array[10] = {};

If you want to declare the array and at once to allocate memory for each element of the array you could write for example

int * array[10] = 
{ 
    new int, new int, new int, new int, new int, new int, new int, new int, new int, new int 
}; 

or

int * array[10] = 
{ 
    new int( 0 ), new int( 1 ), new int( 2 ), new int( 3 ), new int( 4 ), new int( 5 ), new int( 6 ), new int( 7 ), new int( 8 ), new int( 9 ) 
}; 

But in any case it would be better to do the assignment using some loop or standard algorithm because in general the array can have more than 10 elements.

Also you should not forget to delete all allocated memory. For example

std::for_each( std::begin( array ), std::end(array ), std::default_delete<int>() );

Or if you have already defined objects of type int you could write for example

int x0, x1, x2, x3, x4, x5, x6, x7, x8, x9;
//...
int * array[10] = 
{ 
    &x0, &x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8, &x9 
}; 

Such an initialization is used very often for arrays of function pointers.

Comments

3
int **array = new int*[length];

Or, without dynamic memory allocaction :

int *array[10];

4 Comments

This isn't it either... Can someone just tell me if I'm supposed to use operator "NEW" with array of pointers when initializing it... gosh lol
if you don't want dynamic allocation : int *array[50];
the new operator is specifically for dynamically allocating memory off the heap, it isn't statically allocated by the compiler, so using new is a huge difference from the regular way other than you have to specifically delete the memory
@perkes456 This is the answer. You are doing two different things in your question. One is an array of pointers on the stack and the other is a dynamic array of pointers on the heap. This answer gives you both methods, pick the one you want.
0

First int* array[10] would create an array of 10 Int pointers, which would be initlized to garbage values so best practice for that is

int* array[10];
for(int i = 0;i<10;i++)
{
    array[i] = NULL;
}

That will safely unitize all pointers to an appropriate value so that if you try and access one that you haven't stored something in it will throw a seg fault every time.

Second array = new int[10] is creating a pointer to a array of 10 integers which is a completely different thing. If you want to set up an array of pointers using new that would be

int** array = new int*[10];

3 Comments

You don't need a loop to initialize the elements to NULL. See my first comment. i.e. what you're showing is far from the best practice.
Depends on the compiler, older ones like gcc don't have default values for primitives, which usuaslly means what ever fragmented bits were still alive in memory when it was allocated will still be there and the starting value of the variable
As per my comment to OP, int * array[10] = {}; Done. No need for loops.
0

You can have something like this -

int **array = new int*[n](); //n is the number of pointers you need.
//() after declaration initializes all pointers with nullptr

5 Comments

upvoting because & answers the question partially.
@Jean-FrançoisFabre except for the missing initialization of the array of pointers?
I thought he just wanted the declaration syntax. But anyway, int **array = new int*[n](); This initializes the array as well.
it was ambiguous whether he wanted to initialize the arrays or not. But, I agree, this was the next step. I always try to go deeper in basic questions like this otherwise all answers are the same, so I covered that part too.
@Jean-FrançoisFabre Silly me, I must have been confused by the title and "I forgot how to initialize the array of pointers in c++..."
0

You can use

int* a = {new int(....), new int(....) ,...

Or something like below

for (int b = 0; b < 10; ++b)
 a[b] = new int(....);

I think this link has a good explanation about array of pointers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.