0

Pointers can only hold address?

how is this possible? int c; //declaration int* pc = new c; // pointer pointing towards an address in heap *pc=c; ( why not *pc= &c;)

21st Jun 2017, 3:30 PM
Rishabh Singh Rajput
Rishabh Singh Rajput - avatar
2 Respuestas
+ 8
*pc = c; assigns the value of c to the value pointed by the address stored in pc. *pc = &c; is a common mistake made by beginners. (* should not be used here) pc = &c; assigns address of variable c to pointer pc.
21st Jun 2017, 4:37 PM
Hatsy Rei
Hatsy Rei - avatar
+ 2
When declaring your pointer, you can remove the new and it would be valid as well: int c; int* p_c = c;
22nd Jun 2017, 1:03 AM
Zeke Williams
Zeke Williams - avatar