-7

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

1

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;
cdhowie
  • 158,093
  • 24
  • 286
  • 300
-1

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.

Tomaz Canabrava
  • 2,320
  • 15
  • 20
  • 2
    aaaand the `delete` :) – Pierre Apr 10 '17 at 15:08
  • 2
    I feel the use of `new` and `auto` here is distracting from what the example is trying to illustrate since OP is presumably an absolute beginner. – François Andrieux Apr 10 '17 at 15:09
  • i don't know, I could go old fashioned c++03 style but I felt that explaining in modern c++ is easier in a lot of cases. – Tomaz Canabrava Apr 10 '17 at 15:12
  • 1
    Modern C++ discourages the use of `new` and `delete`. I think it would be easier to just use a pointer to automatic storage (i.e. `int i; int* a = &i; ...`). – Kevin Apr 10 '17 at 15:16