Vaelus
Vaelus

Reputation: 1085

Reference to a derefrenced pointer

Is the following standards compliant c++ code?

int *p;
p = new int;

int &n = *p;
n = 5;

Furthermore, am I right in thinking that this code would essentially allocate memory for an int, then give a name, specifically n, to that memory location that could be used like a normal int type variable?

Upvotes: 2

Views: 117

Answers (1)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145457

The given code

int *p;
p = new int;

int &n = *p;
n = 5;

is valid.

I'd write just “yep” as an answer but SO rules – and the sometimes very rule-based users – won't admit such a brief answer.

Upvotes: 4

Related Questions