1

Is it possible to do the following in C?

int *NumPtr = & (int) 44;

Or does this need to be done 'through' an intermediary variable, such as:

int Num = 3, *NumPtr;
NumPtr = &Num;
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
  • What do you expect `int *NumPtr = & (int) 44;` to do? What is it taking an address of? What happens when you try it? I suggest compiling and running to find out. – Code-Apprentice Jan 07 '21 at 05:16
  • `44` is a literal - that is to say, it is not allocated memory in the same way as a variable is. So, getting address for it, is not exactly feasible! – anurag Jan 07 '21 at 05:19

1 Answers1

4

You can use a compound literal:

int *NumPtr = &(int){44};

Compound literals are usually used to create anonymous arrays or structs, but they can be used as here as well.

Barmar
  • 741,623
  • 53
  • 500
  • 612