468,765 Members | 1,212 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

Interesting question on const.

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

Nov 14 '05 #1
9 1273
va******@rediffmail.com wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
<va******@rediffmail.com> wrote in message
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.

--
Tor <torust AT online DOT no>
Nov 14 '05 #3
va******@rediffmail.com wrote:

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.

--
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare
Nov 14 '05 #4
In message <_L******************@news4.e.nsc.no>
"Tor Rustad" <to****@online.no.spam> wrote:
<va******@rediffmail.com> wrote in message
Hi all,
Recently I attended an interview in which the question
"Is there any difference between "const T var" and
"T const var"?


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.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1728 727430
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 14 '05 #5
"Kevin Bracey" <ke**********@tematic.com> wrote in message

<snip>
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>

--
Tor <torust AT online DOT no>
Nov 14 '05 #6
va******@rediffmail.com wrote on 23/03/05 :
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"


Correct.

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

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"

Nov 14 '05 #7
Je***********@physik.fu-berlin.de wrote on 23/03/05 :
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"

Nov 14 '05 #8
On Wed, 23 Mar 2005 11:42:31 +0100, "Tor Rustad"
<to****@online.no.spam> wrote:
<va******@rediffmail.com> wrote in message
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!


Are you sure? This is a typedef, not a #define.
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.


--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #9
"Tor Rustad" <to****@online.no> writes:
<va******@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 - de*@des.no
Nov 14 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by vj | last post: by
16 posts views Thread by makko | last post: by
2 posts views Thread by Niklas Norrthon | last post: by
6 posts views Thread by Claude Yih | last post: by
27 posts views Thread by Frederick Gotham | last post: by
8 posts views Thread by sagi.perel | last post: by
66 posts views Thread by prady | last post: by
11 posts views Thread by onkar.n.mahajan | last post: by
126 posts views Thread by jacob navia | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.