470,833 Members | 1,382 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

What is the use of qualifier const

It seems I never need to use it. It only made a long line longer, and
made you type five more characters. I feel so strange why people are
talking about it.
Jun 27 '08 #1
9 1233
is*********@gmail.com wrote:
It seems I never need to use it. It only made a long line longer, and
made you type five more characters. I feel so strange why people are
talking about it.
Don't you ever create read only variables or string literals or pass
pointers to read only data to functions?

--
Ian Collins.
Jun 27 '08 #2
On Apr 11, 11:33 pm, Ian Collins <ian-n...@hotmail.comwrote:
Don't you ever create read only variables or string literals or pass
pointers to read only data to functions?
I do. But I never used const. I just took care not to modify them. In
fact, I never needed to modify them.

Who will put a qualifier before something and modify this thing
later? And if he dose not modify this thing later, why dose he put a
qualifier before it?

I could not under why people do that. When I saw some old C code, I
found people never used const. Their programs are still robust enough
without const.
Jun 27 '08 #3
is*********@gmail.com wrote:
On Apr 11, 11:33 pm, Ian Collins <ian-n...@hotmail.comwrote:
>Don't you ever create read only variables or string literals or pass
pointers to read only data to functions?

I do. But I never used const. I just took care not to modify them. In
fact, I never needed to modify them.
Then you enjoy skating on thin ice, or never make mistakes.
Who will put a qualifier before something and modify this thing
later? And if he dose not modify this thing later, why dose he put a
qualifier before it?
So the compiler will tell him if he accidentally does attempt to modify it.
I could not under why people do that.
Because they want to write safe, well behaved programs. They want their
constant data stored somewhere it can't be modified. Many embedded
systems have more FLASH than RAM, so it makes sense to store constant
data in FLASH.
When I saw some old C code, I
found people never used const. Their programs are still robust enough
without const.
That's because cost was introduced later. It wouldn't have been
introduced if there wasn't some thing to fix or a perceived need to
differentiate between constant and volatile data.

--
Ian Collins.
Jun 27 '08 #4
On Apr 11, 8:41*pm, istillsh...@gmail.com wrote:
On Apr 11, 11:33 pm, Ian Collins <ian-n...@hotmail.comwrote:
Don't you ever create read only variables or string literals or pass
pointers to read only data to functions?

I do. But I never used const. I just took care not to modify them. *In
fact, I never needed to modify them.
It's a great way to do it. Just like grenades. Don't bother with
putting pins in them, just hold the handles down and they are safe.
Who will put a qualifier before something and modify this thing
later? *
The second programmer, who never saw the qualifier put there.
And if he dose not modify this thing later, why dose he put a
qualifier before it?
To give him an error message to tell him he did something stupid.
I could not under why people do that. *When I saw some old C code, I
found people never used const. *Their programs are still robust enough
without const.
If you substitute the word "fragile" with "robust" then I can agree
with you.
Old code often won't even compile with modern compilers, and the older
it is the less likely it will compile.
If it does succeed in compiling, chances are good it won't run.
If it does run, it is likely to be packed with exploits like gets()
because old time programmers did not have to deal with a hostile
audience.
Old numerical stuff is sometimes villianously bad.
Jun 27 '08 #5
is*********@gmail.com wrote:
On Apr 11, 11:33 pm, Ian Collins <ian-n...@hotmail.comwrote:
>Don't you ever create read only variables or string literals or pass
pointers to read only data to functions?

I do. But I never used const. I just took care not to modify them. In
fact, I never needed to modify them.

Who will put a qualifier before something and modify this thing
later? And if he dose not modify this thing later, why dose he put a
qualifier before it?
Accidents happen. It's unlikely that a programmer who
doesn't intend to modify something will write `*ptr = 42',
but he may well write foo(ptr) without realizing the foo
makes a modification. The prototypical example of this is
perhaps strtok(ptr, " \t\n"), forgetting for a moment that
strtok() might modify what its first argument points at.

Or perhaps at the time the programmer writes foo(ptr)
all is well, because foo() doesn't touch anything. But in
the run-up to the 1.2 release, some other programmer changes
foo() without being aware of its "contract," and ...

If the programmers who work on foo() and on its callers
are scrupulous about their use of `const', errors of this
kind get caught during compilation instead of during the
Big Demo For The Customer.
I could not under why people do that. When I saw some old C code, I
found people never used const. Their programs are still robust enough
without const.
Those old programs were also robust enough without `void'
and without function prototypes, so what's your point?

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #6
On Apr 12, 9:04 am, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
Those old programs were also robust enough without `void'
and without function prototypes, so what's your point?
I think void and function prototypes are useful additions. But for
const, I rarely use it largely because I don't want to type five
extra characters,
'c', 'o', 'n', 's', and 't'. Between pounding five more characters
and taking care not to modify the constant variable, I prefer the
latter.
Jun 27 '08 #7
is*********@gmail.com wrote:
On Apr 12, 9:04 am, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
> Those old programs were also robust enough without `void'
and without function prototypes, so what's your point?

I think void and function prototypes are useful additions. But for
const, I rarely use it largely because I don't want to type five
extra characters,
'c', 'o', 'n', 's', and 't'. Between pounding five more characters
and taking care not to modify the constant variable, I prefer the
latter.
The first time you made this mistake I let it pass as
trivial, but now that you've made it three times: Your
keystroke count is wrong. (So are your priorities.)

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #8
On Sat, 12 Apr 2008 06:16:55 -0700, istillshine wrote:
On Apr 12, 9:04 am, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
> Those old programs were also robust enough without `void'
and without function prototypes, so what's your point?

I think void and function prototypes are useful additions. But for
const, I rarely use it largely because I don't want to type five
extra characters,
'c', 'o', 'n', 's', and 't'. Between pounding five more characters
and taking care not to modify the constant variable, I prefer the
latter.
If you program alone and your code will never be used by anyone but
you then you may have a point. Not a good point but a point.

However if your code is ever used by others then not using const
will cause unnecessary difficulties for these others programmers.

Also if you yourself ever find it necessary to come back 6 months
or a year later to maintain your own code you may find that you
wish you had typed those characters a few times.

Regards
Mike
Jun 27 '08 #9
On 12 Apr, 14:16, istillsh...@gmail.com wrote:
On Apr 12, 9:04 am, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
* * *Those old programs were also robust enough without `void'
and without function prototypes, so what's your point?

I think void and function prototypes are useful additions. *But for
const, *I rarely use it largely because I don't want to type five
extra characters,
'c', 'o', 'n', 's', and 't'. *Between pounding five more characters
and taking care not to modify the constant variable, I prefer the
latter.
http://en.wikipedia.org/wiki/APL_(programming_language)

--
Nick Keighley
Jun 27 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Mahesh Tomar | last post: by
13 posts views Thread by matthias_k | last post: by
15 posts views Thread by Walter Dnes (delete the 'z' to get my real address | last post: by
5 posts views Thread by amvoiepd | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.