C++ – Initializing pointers in C++


Can assign a pointer to a value on declaration? Something like this:

    int * p = &(1000)

Best Solution

Yes, you can initialize pointers to a value on declaration, however you can't do:

int *p = &(1000);

& is the address of operator and you can't apply that to a constant (although if you could, that would be interesting). Try using another variable:

int foo = 1000;
int *p = &foo;

or type-casting:

int *p = (int *)(1000); // or reinterpret_cast<>/static_cast<>/etc