Interesting question on const.

V

vashwath

Hi all,
Recently I attended an interview in which the question "Is there any
difference between "const T var" and "T const var"? was asked.I
answered "NO"(I guessed it:-( ).Did I answered correctly?Please shed
some light on this.I searched the FAQ, but in vain.

Thanks
 
T

Tor Rustad

Hi all,
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.

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.

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!

The const pointer can be initialized to point to an object
(which may be non-const), but cannot safely be changed
to point to another object later. OTOH, the pointer to a
const int object, can safely be changed to point to other
const int objects.
 
J

Jens.Toerring

Recently I attended an interview in which the question "Is there any
difference between "const T var" and "T const var"? was asked.I
answered "NO"(I guessed it:-( ).Did I answered correctly?Please shed
some light on this.I searched the FAQ, but in vain.

As far as I know the answer is "it depends";-) For normal types (char,
int, double etc.) it doesn't make a difference, i.e.

int const i = 3;
const int j = 7;

are equivalent. But when 'T' is a pointer things are different. E.g.

const char *a = "some literal string";

is a pointer to memory you're not supposed to change, i.e. what's
pointed to is constant. This can also be written as

char const *a = "some literal string";

(but that wouldn't fit with the "const T var" or the "T const var"
scheme). On the other hand

char * const b = 0xDEADBEEF;

is a constant pointer, i.e. you can't change the pointer, but what
it's pointing to can be changed.
Regards, Jens
 
C

CBFalconer

Recently I attended an interview in which the question "Is there
any difference between "const T var" and "T const var"? was asked.
I answered "NO" (I guessed it:-( ). Did I answered correctly?
Please shed some light on this. I searched the FAQ, but in vain.

Please put at least one blank after a '.'. It greatly enhances
readability. I did it for you in the above quote.

You are correct. However you are searching in the wrong place, you
should be looking in the C standard. It may require examining the
BNF. Google for N869 to find a free version of the last draft,
which has a text version searchable with grep etc. (The paid final
version is only a pdf, not usable with text tools.) I have a
slightly modified for search/quotation purposes version on my site
as a bz2 compressed file.
 
K

Kevin Bracey

In message <[email protected]>
"Tor Rustad said:
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!

I'm pretty certain that's not right. Typedef doesn't work on a syntactic
level like that. I believe that if T is a typedef for int *, then both cases
are declaring a constant "T", ie a constant "pointer to int":

int * const var;

Now, if T was #defined as "int *" (ew!), then there would be the difference
you described.
 
T

Tor Rustad

I'm pretty certain that's not right. Typedef doesn't work on a syntactic
level like that.

Ooops... I think you are right! That's another good reason to avoid
using typedef to create "pointer type"...


<Pssst ... OP>
replace "typedef" with "alias", and the rest of my response should be
fine.
</Pssst ... OP>
 
E

Emmanuel Delahaye

As far as I know the answer is "it depends";-) For normal types (char,

No, it doesn't.
int, double etc.) it doesn't make a difference, i.e.

int const i = 3;
const int j = 7;

are equivalent.
Yes.

But when 'T' is a pointer things are different. E.g.

Huh ? Why ?
const char *a = "some literal string";

is a pointer to memory you're not supposed to change, i.e. what's
pointed to is constant. This can also be written as

char const *a = "some literal string";

Correct. And is the same pattern than

int const i = 3;
const int j = 7;

or the OP's question.
(but that wouldn't fit with the "const T var" or the "T const var"
scheme). On the other hand

char * const b = 0xDEADBEEF;

is a constant pointer, i.e. you can't change the pointer, but what
it's pointing to can be changed.

Sure, but it's different due to the '*' that was not in the OP's
example.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
A

Alan Balmer

The answer is that it depends.

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.

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!

Are you sure? This is a typedef, not a #define.
 
?

=?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=

Tor Rustad said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top