Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: callback function |
| Date | 2012-10-20 12:47 -0700 |
| Organization | None to speak of |
| Message-ID | <lnhappgc3k.fsf@nuthaus.mib.org> (permalink) |
| References | <k5n77p$e5v$1@speranza.aioe.org> <1o8u781nrpll3im14bf0019tg1523qv226@4ax.com> <k5sl4u$re$1@speranza.aioe.org> <6f84885kvafnr4vmb12o1a4o0tgl9u628p@4ax.com> <k5usd7$s6r$1@speranza.aioe.org> |
"Bill Cunningham" <billcun@suddenlink.net> writes:
> Barry Schwarz wrote:
>> Do you really think the standard should be changed because your
>> limited understanding of the language does not provide you an obvious
>> use of the qualifier?
>
> No not if its that useful. It's because of my limited understanding of C
> that I make this statement. Can you give me a good example of when const is
> good to use. I've never needed it?
The "const" keyword really means "read-only". It's used whenever you
want to assert that a given object's value will not be modified, and
you want the compiler to enforce this for you.
For example, if you have a function whose declaration looks like this:
void func(char *s);
and you call it like this:
char s[] = "Hello, world";
func(s);
then it's possible that the call could change the contents of s.
Sometimes, of course, that's exactly what you want -- but sometimes it
isn't.
For example, given the above declaration you can't safely call the
function with a string literal:
func("Hello, world"); /* dangerous! */
because if func attempts to modify the contents of the string, the
program's behavior is undefined (a crash is likely).
On the other hand, adding a "const" keyword to the declaration:
void func(const char *s);
(nearly) guarantees that func will not attempt to modify the contents of
the string whose address you pass to it. (The "const" applies to what s
points to, not to s itself.)
A good example of this is the standard strlen() function, which computes
the length of a string. It's declared as:
size_t strlen(const char *s);
There's no reason for strlen() to want to modify the string, and good
reasons to guarantee that it won't. And it's perfectly safe to call it
with a string literal:
strlen("Hello, world") /* returns 12 */
My own preference is to declare things as "const" unless there's a
specific need to modify them. For example, suppose I have a function
that takes a char* argument that points to a string, and I want to store
the length of the string in a local object so I don't have to recompute
it:
void frobnicate(char *s) {
const size_t len = strlen(s);
/* ... */
}
If I have some particular need to modify the value of len later on,
obviously I'll omit the "const". But if it's going to retain the
same value, I'll add the "const" keyword, so someone reading the
code and seeing a reference to "len" 42 lines farther down doesn't
have to wonder what its value is at that point -- it's the same
value it had when it was initialized.
Anything you can do with "const", you can do without it. If you took
an entire body of working C code (including the standard library) and
removed every occurrence of "const", it would probably continue to
work correctly. But it would then be very easy to introduce errors,
modifying objects that shouldn't be modified; with "const", the
compiler will catch many such programming errors. "const" is a way
of giving the compiler extra information about your code, information
that can help the compiler help you keep the code correct.
(If I were designing a new language from scratch, I'd make "const"
or "read-only" the default for declared objects; you'd need an
extra keyword, perhaps "var", to indicate that an object's value
can be changed. But that wouldn't be C.)
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-17 17:16 -0400
Re: callback function John Gordon <gordon@panix.com> - 2012-10-17 21:36 +0000
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-17 17:44 -0400
Re: callback function John Gordon <gordon@panix.com> - 2012-10-17 21:51 +0000
Re: callback function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-18 00:11 +0100
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-18 18:37 -0400
Re: callback function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-19 01:16 +0100
Re: callback function Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-10-20 06:48 -0700
Re: callback function Ian Collins <ian-news@hotmail.com> - 2012-10-18 10:45 +1300
Re: callback function pete <pfiland@mindspring.com> - 2012-10-19 14:49 -0400
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-19 16:59 -0400
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-19 17:04 -0400
Re: callback function Barry Schwarz <schwarzb@dqel.com> - 2012-10-19 14:14 -0700
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-19 17:31 -0400
Re: callback function Keith Thompson <kst-u@mib.org> - 2012-10-19 14:54 -0700
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-19 18:42 -0400
Re: callback function James Kuyper <jameskuyper@verizon.net> - 2012-10-17 17:47 -0400
Re: callback function tom st denis <tom@iahu.ca> - 2012-10-19 04:44 -0700
Re: callback function James Kuyper <jameskuyper@verizon.net> - 2012-10-19 08:24 -0400
Re: callback function Barry Schwarz <schwarzb@dqel.com> - 2012-10-17 14:39 -0700
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-17 17:50 -0400
Re: callback function Ian Collins <ian-news@hotmail.com> - 2012-10-18 11:00 +1300
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-18 18:20 -0400
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-18 18:24 -0400
Re: callback function Barry Schwarz <schwarzb@dqel.com> - 2012-10-18 22:30 -0700
Re: callback function Ian Collins <ian-news@hotmail.com> - 2012-10-20 11:58 +1300
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-19 19:27 -0400
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-19 19:43 -0400
Re: callback function Barry Schwarz <schwarzb@dqel.com> - 2012-10-19 21:09 -0700
Re: callback function Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-10-20 06:53 -0700
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-17 19:25 -0400
Re: callback function Ian Collins <ian-news@hotmail.com> - 2012-10-18 13:34 +1300
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-17 20:41 -0400
Re: callback function Ian Collins <ian-news@hotmail.com> - 2012-10-18 14:37 +1300
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-18 18:17 -0400
Re: callback function Ian Collins <ian-news@hotmail.com> - 2012-10-20 11:54 +1300
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-18 18:54 -0400
Re: callback function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-19 01:23 +0100
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-18 20:32 -0400
Re: callback function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-19 02:43 +0100
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-19 16:56 -0400
Re: callback function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-19 22:10 +0100
Re: callback function Barry Schwarz <schwarzb@dqel.com> - 2012-10-18 22:30 -0700
Re: callback function Kenneth Brody <kenbrody@spamcop.net> - 2012-10-19 12:37 -0400
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-19 16:52 -0400
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-19 18:44 -0400
Re: callback function Barry Schwarz <schwarzb@dqel.com> - 2012-10-19 21:09 -0700
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-20 15:01 -0400
Re: callback function Keith Thompson <kst-u@mib.org> - 2012-10-20 12:47 -0700
Re: callback function Les Cargill <lcargill99@comcast.com> - 2012-10-20 14:51 -0500
Re: callback function Barry Schwarz <schwarzb@dqel.com> - 2012-10-20 14:39 -0700
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-20 19:06 -0400
Re: callback function Barry Schwarz <schwarzb@dqel.com> - 2012-10-20 22:22 -0700
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-21 14:35 -0400
Re: callback function Kenneth Brody <kenbrody@spamcop.net> - 2012-10-22 10:58 -0400
Re: callback function Bill Cunningham <billcun@suddenlink.net> - 2012-10-24 17:28 -0700
Re: callback function Ian Collins <ian-news@hotmail.com> - 2012-10-21 11:32 +1300
Re: callback function Keith Thompson <kst-u@mib.org> - 2012-10-20 15:51 -0700
Re: callback function Robert Miles <milesrf@Usenet-News.net> - 2012-11-25 22:39 -0600
Re: callback function Keith Thompson <kst-u@mib.org> - 2012-10-17 17:51 -0700
Re: callback function Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-10-18 20:34 +0300
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-18 18:15 -0400
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-18 18:27 -0400
Re: callback function Keith Thompson <kst-u@mib.org> - 2012-10-18 15:47 -0700
Re: callback function "Bill Cunningham" <billcun@suddenlink.net> - 2012-10-18 18:52 -0400
Re: callback function blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2012-10-25 10:01 +0000
Re: callback function Kenneth Brody <kenbrody@spamcop.net> - 2012-10-25 12:44 -0400
Re: callback function blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2012-10-26 12:48 +0000
Re: callback function Robert Miles <milesrf@Usenet-News.net> - 2012-11-25 22:13 -0600
Re: callback function Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-11-26 03:42 -0800
Re: callback function James Kuyper <jameskuyper@verizon.net> - 2012-11-26 06:59 -0500
Re: callback function Keith Thompson <kst-u@mib.org> - 2012-11-26 23:03 -0800
csiph-web