0

Is pointer a variable or an address?

Because int** array= new int*[4] is so confusing.

For int** array in this code, the pointer becomes variables to store the pointer addresses, and new int*[4] becomes addresses that will be stored in another pointer.

What am I understanding wrong?

3
  • 1
    A pointer is a variable. It's value is an address. Just like you have int variables whose values are integers. It's no different. Commented Nov 8, 2020 at 13:46
  • I'm not sure you are understanding anything wrong, although your teminology is a bit strange. Variables are variables, pointer or not they're all the same, except that they hold different types of value. I think what is confusing you is that a pointer can point to anything, including another pointer. That's what int** array is, it's a pointer whose value is the address of another pointer, whose value is the address of an integer. It's a double pointer. Commented Nov 8, 2020 at 13:54
  • 1
    Here's another perspective, how is this (which you don't understand) int** a = new int*[4] any different from this (which I'm assuming that you do) int* a = new int[4]? What in your mind is the reason that the first is hard to understand and the second is OK? If you can allocate int why should you not be able to allocate int*? That is the only difference between the two statements, Commented Nov 8, 2020 at 14:02

3 Answers 3

1

Pointer is a data type, similar to int or char. The data here is a memory address. Pointers are qualified with the types of values expected to be located in the memory at the referred addresses. As other data types, pointers could be used as types for variables, function arguments, array elements etc.

So, int **array is a variable of type int ** which means a pointer to a value of type int * located somewhere in the memory. In turn, int * means a pointer to a value of type int located somewhere in the memory. Or, in short, int ** is a pointer to a pointer to an int.

Expression new int*[4] means allocate in the memory an object of type int *[4] and return a pointer to it. Type int *[4] means an array of four elements of type int * where int *, as we already know, means a point to int value.

So, in the initialization, the types of the left part (int **) and of the right part (int *[]) are different. However, C++ is able to automatically convert arrays into pointers in case the type of array element is the same as the type of the value referred by the pointer. In our case, array element type is int * and the same is the type referred by the pointer. When an array is converted to a pointer, the resulting pointer will refer to the first element of the array.

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

Comments

1

In this declaration

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

array is a variable that is initialized by the address of the first element of a dynamically allocated array with the element type int *.

If you have an array declared like

T array[N];

where T is some type then a pointer to the first element of the array can be declared like

T *p = array;

If to take into account your array then just T is equal to int *.

Comments

1

Simply put, a pointer is a variable that can store the address of another variable instead of an int or a float or some other value like a "normal" variable.

Like any other variable, the pointer itself also has an address so you can have a pointer to pointer, i.e. a pointer that stores the address of another pointer.

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.