While reading some code about copy constructors in C++, I found a statement like this *ptr=len
(where len
is an int
type variable and *ptr
is a int
type pointer).
What does this mean?
While reading some code about copy constructors in C++, I found a statement like this *ptr=len
(where len
is an int
type variable and *ptr
is a int
type pointer).
What does this mean?
It is assignment to the value/location pointed to by ptr
. To put it another way, we are assigning the value of len
to the value that ptr
points to.
For example:
// Declare and initialize int variable.
int x = 0;
// Declare pointer-to-int variable, initialize to be pointing at x.
int *xp = &x;
// Assign to the value _pointed to by_ xp, which is x. In other words,
// assigning to *xp is the same thing as assigning to x.
*xp = 1;
// Will display 1, because x was reassigned through *xp.
std::cout << x << std::endl;
it means that the contents of the ptr variable is not the value of len.
in other words, the value of the variable pointed out by ptr, has the value of len.
int foo;
int *a = &foo; // a points to the memory of foo.
int value = 10;
(*a) = value; // changes the value of foo via pointer indirection.
now (*a) equals 10.