2

I would like to create a pointer to a value in one line; I want the same functionality as this:

int i = MY_VALUE
int * j = &i;

However, I want to do this in one line, and do not want to use two variables. I know that I can do this:

int * i = new int (MY_VALUE);

But I don't want to use dynamic memory; I want to use static memory.

Is there a way that I can allocate a pointer to a value, statically, with one variable, in one line?

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
ostrichofevil
  • 749
  • 7
  • 19
  • 4
    Is there really a surge or random questions in C++ today, or is it just me? – SergeyA Apr 13 '16 at 21:13
  • 1
    Is this question about C or C++ ? There's a duplicate for C [here](http://stackoverflow.com/questions/24805673/declare-and-initialize-pointer-concisely-i-e-pointer-to-int). – Quentin Apr 13 '16 at 21:16
  • 2
    Can you explain why you need it to be on one line and without using two variables? That might help us come up with an acceptable solution... – Brian Bi Apr 13 '16 at 21:17
  • @Quentin It's about C++, but I thought that the answers might be the same for both languages. – ostrichofevil Apr 13 '16 at 21:18
  • @Brian Of course, if it's not possible to do what I want to do, I can use two variables. I wanted to do it with one variable because of the potential overhead of two variables when there could be one. – ostrichofevil Apr 13 '16 at 21:19
  • You should really add more context, because I doubt there is anything to be gained from having only one variable. In fact, since that one variable would be the pointer, there **has** to be an actual variable somewhere for it to point to, even if it is not named in the code. – Quentin Apr 13 '16 at 21:25
  • I removed the C flag, it is really too different from C++ in these points to be relevant, here. Also, you are asking about "static" memory, this seems to point to a confusion what static means. – Jens Gustedt Apr 13 '16 at 21:29
  • OP: One thing to think about is that a pointer is like a street address to a house. No matter how you (legally) declare it, if you have an "address", you will (should) have a "house" in memory [otherwise you get UB]. In other words, for all intents and purposes, you will have two "variables" in memory. Also, @SergeyA, it's not just you. The main reason for the surge is that we're a few weeks into a new college quarter. – CodeMouse92 Apr 14 '16 at 00:40
  • @JasonMc92, thanks for info and confirming I am not seeing things :) – SergeyA Apr 14 '16 at 14:44

2 Answers2

4

If you must...

int i = 5, *p = &i;
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
2

No, you cannot have a pointer to a value, you can only have a pointer to a variable or pointer to an object (lvalue).

To elaborate, from C prespective, quoting C11 chapter §6.5.3.2 for the usage of & operator

The operand of the unary & operator shall be either a function designator, the result of a [] or unary * operator, or an lvalue that designates an object that is not a bit-field and is not declared with the register storage-class specifier.

So, something along the line of

int * p = &10;

is purely invalid.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261