-1

I have found the const rules in c to be very confusing. I want to know if there are known rules or a way to know what's allowed when dealing with const/pointers to const.

I will give an exmple:

Not allowed:

const int b=3;

int * const a=&b;

Allowed:

int b=3;

int * const a=&b;

Are there rules to know before compiling if the code that contain const will compile? One thing I though about is -- and I want to know if it's a rule -- every time you write a line with const, does there always have to be initialize after it? I know that there is some rule about what const can/can't hold.

Carey Gregory
  • 6,836
  • 2
  • 26
  • 47
user3250354
  • 101
  • 10
  • What's the problem? You can convert a `T *` to a `U *` iff `U` is an equally or more CV-qualified version of `T`. – Kerrek SB Jan 29 '14 at 19:28
  • 1
    -1. You can be reasonably expected to spell the only important word in your question correctly. – djechlin Jan 29 '14 at 19:30
  • `int *const` is a constant pointer to a variable int, you seem to be looking for a variable pointer to a constant int (`const int *` or `int const *`) or a constant pointer to a constant int (`const int *const`). – Kevin Jan 29 '14 at 19:31
  • possible duplicate of [When to use const\* versus const \* const?](http://stackoverflow.com/questions/21097690/when-to-use-const-versus-const-const) – Caleb Jan 29 '14 at 19:45

3 Answers3

2

Your problem isn't really with const itself, it's with not being able to understand what variables hold.

declaring something as const means that the value will not be changed. So:

const int b=3;

Now b holds 3 and you can't assign it something else, because it's constant. The same holds true for pointers:

int b=3;
int * const a=&b;

The pointer a is now pointing to the address of b, you can't assign a different address there now. The reason that this:

const int b=3;
int * const a=&b;

doesn't work is because a is a constant pointer to a (non-constant) int. Yet you're trying to assign a the value of a constant int. So the compiler says, "no".

const int b=3;
const int * a=&b;

This change now allows it to work, because a is a pointer to a constant-int, which is what b is, so it's OK to make the assignment. If you want both a and b to be constant you need to make the second line:

const int * const a=&b;

You're running into an issue with the spiral rule.

Mike
  • 47,263
  • 29
  • 113
  • 177
1

First example:

const int b=3;
// int * const a=&b; ERROR: cannot cast "const int *" to "int *".
const int *const a = &b;

You have to unwrap the declaration.

  • The type int *const is a constant pointer to a mutable int.
  • The type const int * is a mutable pointer to a constant int.
  • The type const int *const is a constant pointer to a constant int.

You can't assign if the left-hand side of the assignment is a constant (by which I mean, "has a type with the const qualifier"). So,

int *x = ...;
x = ...; // ok, RHS must be int *
*x = ...; // ok, RHS must be int

int *const x = ...;
x = ...; // error
*x = ...; // ok, RHS must be int

const int *x = ...;
x = ...; // ok, RHS may be int * or const int *
*x = ...; // error

const int *const x = ...;
x = ...; // error
*x = ...; // error
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
1

The basic rules are:

  • It is okay to add const (when casting or otherwise converting).
  • It is okay to remove const only if it is a const that was added later, not a const that an object was initially defined with.

Examples:

int a = 3;
const int b = 4;

// Okay, adds const (says the int is const).
const int *p0 = (const int *) &a;

// Okay, adds const (says the pointer to the int is const).
int * const p1 = (int * const) &a;

// Okay, removes an added const.
int *p2 = (int *) p0;

// Not okay, removes a defined const.
int *p3 = (int *) &b;
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312