** operator

A very basic question. What does the ** operator in C++ do? For example:

int **matrix;

It appears to be a pointer to a pointer. In the above example, it may be the right * indicates a pointer to a string (holding the address of the first character of the string), but what does the left * indicate? A pointer to the pointer?

It's not exactly an operator on its own - it's just two * operators put together.

In this case, it is declaring a pointer to a pointer to an int. Such an object can be used for a 2D array (basically an array of arrays).
Last edited on
In a declaration like

int** matrix;

* isn't an operator, it's a declarator: the * declarator (pointer declarator). No operator gets called here; the * just tells the compiler you want the variable "matrix" to be a pointer to pointer to int.

But here, when * is used to dereference a pointer

int* pelem = *matrix;

or double dereference it

int elem = **matrix;

then it's the pointer operator (or indirection operator/dereference operator)

Same deal with &

int& count = getCount();

Here & is the reference declarator.

int* pint = &some_int_value;

Here it's the address-of operator (the name "reference operator" is also used, though it's not in the C++ standard.)

Last edited on
Topic archived. No new replies allowed.