Re: Interesting question on const.

From: Dag-Erling Smørgrav (des_at_des.no)
Date: 03/24/05


Date: Thu, 24 Mar 2005 00:43:04 +0100


"Tor Rustad" <torust@online.no> writes:
> <vashwath@rediffmail.com> wrote in message
> > Recently I attended an interview in which the question
> > "Is there any difference between "const T var" and
> > "T const var"?
> The answer is that it depends.

No.

> Let say T is a typedef for "int", then we have
>
> const int var; /* declare read-only int */
> int const var; /* declare read-only int */
>
> which is exactly the same thing. "const" type-qualify
> it's left, but the special case of "const" being first in
> the declaration, it type-qualify it's right.

Yes.

> Now let say T is a typedef for "int *". Then we have
> these two cases:
>
> int * const var; /* declare read-only int pointer */
> const int *var; /* declare pointer to read-only int */
>
> In the first case, the int pointer is const, while in the
> letter case, int is const. This isn't the same thing!

No. The const qualifier applies to the entire typedef. Both variants
are interpreted as "int * const var".

The best way to avoid this confusion is to always place const on the
right of the type it qualifies.

If T were an unparanthesized macro, and not a typedef, your answer
would be correct, but I'm pretty sure that's not what the OP was
asked, and in all likelihood, the interviewer was following a script
and would not have understood a complete answer.

Here's a program which demonstrates this:

 1: typedef int * T;
 2: const T a;
 3: T const b;
 4: int main(int argc, char *argv[])
 5: {
 6: a = &argc;
 7: *a = 1;
 8: b = &argc;
 9: *b = 1;
10: return 0;
11: }

As written, it results in the following errors:

const.c: In function `main':
const.c:6: error: assignment of read-only variable `a'
const.c:8: error: assignment of read-only variable `b'

if you replace the first line with

 1: #define T int *

you get the following:

const.c: In function `main':
const.c:7: error: assignment of read-only location
const.c:8: error: assignment of read-only variable `b'

DES

-- 
Dag-Erling Smørgrav - des@des.no


Relevant Pages

  • Re: Question about array declarators
    ... int main ... fact that foopoints to a const int, ... Those messages are from compilation of the version of the code that you originally posted. ... You think that the 'const' in foo dissallows assignment if the rvalue points to a type that isn't const-qualified. ...
    (comp.lang.c)
  • Re: Question about array declarators
    ... type with "const int". ... But gcc produced nothing. ... of an assignment operator. ...
    (comp.lang.c)
  • RE: function overloading, parameter list error
    ... However, the const in "Type foo(int x, int y) const" only indicates that ... foo does not change it's object memory. ... method you are calling the assignment? ...
    (microsoft.public.vc.language)
  • Re: Pointer / Const Warnings
    ... const int* to an int* which shouldn't happen. ... compiler noticing and complaining. ... This kind of assignment is I think a real ...
    (comp.lang.c)
  • Re: how many times the printf will be executed ?
    ... In any given instance of the program printf will be executed once. ... Good luck in the interview or with your assignment. ...
    (comp.lang.c)