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 9 1383 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
<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> 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
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/
"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> 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"
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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: vj |
last post by:
Hi! I recently came across this intresting behaviour shown by Visual
C++ 6.0 compiler regarding Copy Constructors. Please tell me that is
this the standard behaviour shown by all compilers or its...
|
by: makko |
last post by:
Hello,
anyone know how to writre a program that take a commandline formula
and prints the calculated result? example;
$program 1+(2x3(3/2))-8
reagrds;
Makkko
|
by: Niklas Norrthon |
last post by:
I want to share a technique I recently have found to be useful
to get around some obstacles that data protection can raise.
Consider the following class:
// foo.h
#ifndef H_FOO
#define H_FOO...
|
by: Claude Yih |
last post by:
Hi, everyone. I noticed an interesting thing about fread() this
afternoon. Well, I can't see why so I post this message in the hope of
getting some explanation. Please help me.
I wrote the...
|
by: Frederick Gotham |
last post by:
I thought it might be interesting to share experiences of tracking down a
subtle or mysterious bug. I myself haven't much experience with tracking
down bugs, but there's one in particular which...
|
by: sagi.perel |
last post by:
I have tried to compile the following code on Win & Unix. Doesn't work
on either.
<----- CODE ----->
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include...
|
by: prady |
last post by:
hi all,
could any one solve the following C program. If any one knows the
answer please post it
Ques:
A C function that will print 1 to N one per each line on the
stdout , where N is a int...
|
by: onkar.n.mahajan |
last post by:
Is it possible to from function call on fly in C programming
language ?
I am faced with an interesting problem in that I need to take function
name and arguments on fly (actually from Database...
|
by: jacob navia |
last post by:
Buffer overflows are a fact of life, and, more specifically, a fact of
C.
All is not lost however. In the book
"Value Range Analysis of C programs" Axel Simon tries to establish a...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |