André Hincu
André Hincu

Reputation: 427

playing with references and pointers in C++

I am trying to learn C++ and I'm having some difficulties learning pointers and references. I'm trying to understand why some of the below do not work, and I cannot seem to figure out the difference between "double *const ptd" and "const double *ctd"

double d;
  const double r; //bad; r must be initialised
  const double pi = 3.1416;
  double *ptr = π //illegal to point to a constant, because otherwise one could change the value of the constant which defies the purpose of a constant
  double *const cpt; //bad; cpt must be initialised
  double *const ptd = &d;
  const double *ctd = &d;
  const double *ptc = π
  double *const ptp = π //illegal
  const double *const ppi = π 
  double * const * pptr1 = &ptc;
  double * const * pptr2 = &ptd;

  void F () {
    ptr = new double;
    r = 1.0;
    *ptr = 2.0;
    cpt = new double;
    *cpt = 3.0;
    ptc = new double;
    *ptc = 4.0;
    ptd = new double;
    *ptd = 5.0;
    ctd = new double;
    *ctd = 6.0;
    ptp = new double;
    *ptp = 7.0;
    ppi = new double;
    *ppi = 8.0;
  }

Upvotes: 1

Views: 220

Answers (5)

Mike Seymour
Mike Seymour

Reputation: 254421

double * const (read as "constant pointer") means that the pointer is constant. You can't modify it, but you can change the object that it points to.

const double * or double const * (read as "pointer to constant") means that the pointer can't be used to change the object it points to; but the pointer itself can be modified.

In general, the const qualifies the thing immediately before it; or the first thing, if there's nothing before it.

Upvotes: 4

Zane
Zane

Reputation: 926

Note that part of the problem might be ambiguities in the const syntax. The rule is const applies to whatever is on its immediate left (other than if there is nothing there in which case it applies to whatever is its immediate right)

So const T is the same as T const, but T const * ist not the same as T * const.

This gets worse in understanding when you have several const in a type.

Therefore I will usually always put the const to the right of the type it applies to, to be consistent.

As most people will see examples like const int as the first examples for using constness, it probably puzzles them a lot later on where to put the const

Upvotes: 0

Xiao Jia
Xiao Jia

Reputation: 4259

  • const double * x is the same as double const * x.
    • It means "x points to a double that is immutable"
    • that is, the double value can't be changed via x
  • double * const y
    • means "y is an immutable pointer to a double"
    • you can change the double value via y
    • but you can't change the pointer y itself (i.e. cannot point to another memory location)
  • There's also const double * const z (or double const * const z)
    • means "z is an immutable pointer to an immutable double"
    • you can't change the pointer z itself
    • nor can you change the value via z.

In all the cases, const qualifies the thing immediately before it.

Upvotes: 0

SingerOfTheFall
SingerOfTheFall

Reputation: 29966

First of all, about the difference between double * const ptr and const double * ptr. The first one (double * const ptr) makes a constant pointer ptr which should point to double. The pointer itself is const, what it points to is not const.

The second variant (const double * ptr) makes a pointer that should point to a const double. In this case it's the opposite: the pointer itself is not const, but what it points to is const.

Now, considering the errors you are getting (I'll go from top to bottom):

  • const double r; - you need to initialize constants, like this: const double r = 15;
  • double *ptr = π - You are basically making a pointer to r. Since r is const, the pointer must be a pointer to const double, not just double. The correct one will be const double *ptr = π
  • double *const cpt; - uninitialized constant again, same as the first one. All constants must be initialized. Something like double *const cpt = new double will do.
  • double *const ptp = π - same as the second one. Your pointer here is const, but it has to point at a normal double. Instead, you are truing to make it point on a const double, which leads to an error. const double *const ptp = π will work.
  • double * const * pptr1 = &ptc; Here you are trying to make a pointer pptr1 which should point to a pointer to a double.

Upvotes: 1

Stephane Rolland
Stephane Rolland

Reputation: 39896

const double *ctd:

  • it means a pointer (an address much like a integer value indicating the beginning of a memory zone) which is not constant ( this adresse value can change so as to point to another zone of memory, for example ctd = nullptr;) pointing to a const double..., the double that is pointed will never change its value.

double *const ptd:

  • it means a const pointer(an address much like a const integer value indicating the beginning of a memory zone) which is constant ( this address value CANNOT change so as to point to anything else) pointing to a double which value can change.

Upvotes: 1

Related Questions