1

I am new to c++ programming and trying to understand this syntax

int **arr;

arr = new int *[5];

I am confused about this part new int *[5]; Does it mean pointer of type int to 5 subpointers?

Any help will be appreciated.

1
  • 1
    Does it mean pointer of type int to 5 subpointers? More like a pointer of type "int pointer", and a 5 element array with that. Commented Jul 16, 2015 at 19:59

3 Answers 3

3

This will hopefully become clear when you read it piece by piece:

new        int *             [5]
^^^        ^^^^^             ^^^
give me    pointers to int   and five of those.

Then you safe the address of the first of the new pointers in arr and thus have an dynamically allocated array of five pointers.

Note that "in the real world" (i.e. if no teacher/prof forbids you to) you would use an std::vector instead of new[].

Sign up to request clarification or add additional context in comments.

Comments

2

You are declaring an array of pointers to integers.
The array capacity is 5 (pointers).
The variable is allocated in dynamic memory because of new.

Comments

1

When you have problem with a non trivial type use typedef (at least in your head):

typedef int* int_ptr;
int_ptr *arr;
arr = new int_ptr[5];

It is easier to understand now, is it not?

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.