int** numberLists = new int*[count];

I'm having difficulties understanding this line of code, I've just started programming in c++.

I think I got the basics of pointers but I can't find any helpful "enough" info about arrays of pointers.

I understand that an array of pointers to integers is assigned to variable numberLists which is a pointer to a pointer to an integer. Unfortunately that doesn't get me very far.


9 Years
Discussion Span
Last Post by death_oclock

My best guess, without seeing this in context, is that it is to define a two-dimensional array. It is very unlikely that you will see pointers to single ints (unless you're trying to pass them by reference the 'C' way). In this case, you have one large array with lots of pointers in it. Each one of the "smaller" pointers actually refers to another array. That way you have two dimensions represented.


Thanks for the quick reply, it's starting to make more sense :>
Okay so lets say I declare it as follow:

int** numberLists = new int*[2];

How do I access and fill the "2d" array with the following integers:

2 4
6 8

I'm really confused :S


Ignoring the task of initialization (for now), you would access it like so: numberLists[row][col]; . To get the number 4 for example, the code would be numberLists[0][1]; (1st row, 2nd column).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.