473,384 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,384 software developers and data experts.

const confusion

I have been trying to educate myself with all these different uses of const,
i.e.

char *p
const char *p
char * const p
const char * const p
However, there are a few web pages out there that use some really complex
examples:http://c-faq.com/decl/spiral.anderson.htmlI guess these examples
could be all relevant, however, what's the purpose of such use of so many
const and is there an easier way to solve whatever solution? IMPORTANTALSO,
is there a performance penalty when using all these const? Or isit an
increase? I am thinking an increase, but because it looks so convolutedit
could be slow?Lastly, those function pointers, with const pointerscan be
really hard to read. I know I would use a different and more clearer naming
convention than those in the examples.
Jul 11 '06 #1
2 1691
smnoff posted:
I have been trying to educate myself with all these different uses of
const, i.e.

char *p

Read from right to left.
p is a pointer to a char.

const char *p

p is a pointer to a char which is const.

(Note that it can also be written as: char const *p, in which case you'd
read it as:

p is a pointer to a const char.

char * const p

p is a const pointer to a char.

const char * const p

p is a const pointer to char which is const.

(Note that it can also be written as: char const * const p, in which case
you'd read it as:

p is a const pointer to a const char.
--

Frederick Gotham
Jul 11 '06 #2


smnoff wrote On 07/11/06 11:38,:
I have been trying to educate myself with all these different uses of const,
i.e.

char *p
const char *p
char * const p
const char * const p
However, there are a few web pages out there that use some really complex
examples:http://c-faq.com/decl/spiral.anderson.htmlI guess these examples
could be all relevant, however, what's the purpose of such use of so many
const and is there an easier way to solve whatever solution?
The transition from `int x = 42;' to `const int x = 42;'
is (I hope) easy to understand: The second is just like the
first, with the additional information that x is not to
be changed after it's initialized. x is "read-only."

But C's declarations can be more complex. If we start
with `char *p = "Hello";' there are at least two different
things we might want to make read-only: we might want to
say that p itself is read-only (it will always point to
"Hello"), or that the thing p points to is read-only (so
p cannot be used to replace H with J). Think about this
a bit, and you'll see that "p is read-only" and "*p is
read-only" are different, and are independent (we might
even want to say "both p and *p are read-only").

That's the purpose of having different ways to insert
`const' in a declaration: to allow you to express different
things about what things are and aren't read-only. As for
an "easier way to solve whatever solution," I'm afraid I
can't make sense of the question.
IMPORTANTALSO,
is there a performance penalty when using all these const? Or isit an
increase? I am thinking an increase, but because it looks so convolutedit
could be slow?
Why do you think this is IMPORTANT? Have you made any
measurements of the time penalty for using or omitting `const'?
If you have not, what evidence is there to call it IMPORTANT
rather than TRIVIAL or even NONEXISTENT?

"Look after the pennies, and the pounds will take care
of themselves." Good advice in many circumstances, but not
a universal commandment to fret about minutiae. Or as a friend
of mine once put it: It's always good to get the bottle caps
off the beach so the sand will be nice and clean around the
rotting whale carcase.
Lastly, those function pointers, with const pointerscan be
really hard to read. I know I would use a different and more clearer naming
convention than those in the examples.
With experience you will find them easier to read. There
are only a few common "useful" patterns, and you will learn
to recognize them after a while. Richard Heathfield's post
in the "qsort() not sorting -:(" thread is about the most
complicated const-ing you're likely to run into, or want to
write.

--
Er*********@sun.com

Jul 11 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Zork | last post by:
Hi, I am a little confused with const functions, for instance (here i am just overloading the unary+ operator) if i define: 1) Length Length :: operator+ ( void ) const {return * this;} ... I...
14
by: mandydhaliwal | last post by:
Hi Guys, I was going through a product's source code. They never use const function arguments with default value. for instance Never noticed following type of declaration foo ( const int...
7
by: Andrej Prsa | last post by:
Hello, everyone! When a const int * argument is passed to a function, i.e. int f (const int *var) { printf ("%d\n", *var); } int main ()
9
by: vashwath | last post by:
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...
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
7
by: siddhu | last post by:
Dear Experts, I have a question. I wrote the following code class A { public: void g(){}
9
by: Peithon | last post by:
Hi, This is a very simple question but I couldn't find it in your FAQ. I'm using VC++ and compiling a C program, using the /TC flag. I've got a function for comparing two strings int...
3
by: rwf_20 | last post by:
Hi, I'm looking at the differences between: const NonTrivialObject& obj = functionThatReturnsANonTrivialObjectByValue(); and: const NonTrivialObject obj =
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.