[cs13001] Another question in pointers.

Mikhail Nesterenko mikhail at cs.kent.edu
Mon Nov 12 16:22:18 EST 2012


> 
> I was wondering, what's the difference between:
> 
> p1 = new int;    

this line dynamically allocates a memory location that holds an
integer variable and assigns the address of this memory location to "p1"


> and *p1 = new int;     Is the second one even possible?

this line also dynamically allocates an integer memory location but
assigns its address to wherever "p1" points to (p1 is
derefernced). For this line to compile, the type of "p1" should be a
pointer to a pointer to an integer.

For example:

int **p1, *p2;

p1 = &p2;      // p1 points to p2
*p1 = new int; // makes p2 point to the newly allocated integer


Thanks,
--
Mikhail



More information about the cs13001 mailing list