c equivalence of const_cast?

B

Ben Pfaff

Chris Torek said:
There are two other semantic differences I would consider
significant enough to mention:

- C++'s "const" declares actual constants by default, and
- the default linkage of C++ "const" identifiers is different.

Oh, yes, I temporarily forgot about those. Thank you for
reminding me.
 
O

Old Wolf

Nicolas Pavlidis said:
In C doesn't exsist a const keyword

If you mean "the const keyword doesn't exist in C" then you're
wrong (otherwise, I don't know what you were trying to say).
See N869 6.4.1#1 (the list of keywords). I don't have a copy
of C89 so I can't give you a reference to that, but read
K&R2 if you have it.
normally, some compiler support const variables, like gcc,

Compilers must support it
but IMHO it is the best way to remove the const from the source,
and replace it with defines.

Many people argue the opposite (defines circumvent scope and
linkage rules and type-checking). See the thread "What Value
is Offered by Const-Correctness" in c.l.c++ at the moment,
much of it applies to both languages.

Anyway, how would you use a define to avoid the OP's problem,
which was something like:

int some_func(char *ptr); /* does not modify ptr */
char const *other_func(void);

void foo()
{
char *ptr = other_func();
some_func(ptr);
}
 
C

Chris Barts

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chris Torek wrote:
|>
|>>[const] just isn't the same keyword as in C++, which probably
|>>confuses some people.
|
|
| In article <|
|>It isn't? `const' has largely the same meaning in C and in C++
|>as far as I know. There are some differences in which
|>conversions that change const-ness are allowed implicitly.
|
|
| There are two other semantic differences I would consider
| significant enough to mention:
|
| - C++'s "const" declares actual constants by default, and
| - the default linkage of C++ "const" identifiers is different.
|
| (These two are related, unsurprisingly -- "extern const" in C++
| means what "const" does in C if the variable is not initialized.
| If the variable *is* initialized, though, the C++ "const" is
| still a constant.)
|
| In particular, the following is invalid in C:

Is it still invalid under C99, with VLAs? (I understand that `C'
defaults to C89/C90, but this could possibly confuse someone who
actually had a C99 compiler.)

|
| const int N = 10;
| int a[N];
| int main(void) {
| /* ... code using the array "a" ... */
| return 0;
| }
|

~From what I read of the post you snipped, it seems that Nicloas Pavlidis
should say that `const'-ness in C isn't as strong as it is in C++ or
Pascal or Ada, for example, and that if he wants to avoid /all/
potential surprises arising from the different semantics, he should use
`#define's in C where he would use constant variables in Ada.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBdGd3KxatjOtX+j0RAruSAJ91WVoe4BPg5iQuffAQ5RGyX7uC9wCfQaWo
ftLqRdBJYjOk3NG3XpvXgGM=
=kr6o
-----END PGP SIGNATURE-----
 
K

Keith Thompson

Chris Barts said:
Chris Torek wrote: [...]
| In particular, the following is invalid in C:

Is it still invalid under C99, with VLAs? (I understand that `C'
defaults to C89/C90, but this could possibly confuse someone who
actually had a C99 compiler.)

|
| const int N = 10;
| int a[N];
| int main(void) {
| /* ... code using the array "a" ... */
| return 0;
| }
|

A C99 variable length array cannot have static storage duration
(C99 6.7.5.2p2).
 
M

Mark McIntyre

Maybe I'm wrong, but AFAIK const is not supported by C89, if I'm wrong
tell me!

You're wrong. It just isn't the same keyword as in C++, which probably
confuses some people.
So I recommended to use #defines instead of const

#defines are (in C) totally different to consts. I do agree tho that tehy
behave more like C++'s const objects.
 
M

Mark McIntyre

It isn't? `const' has largely the same meaning in C and in C++
as far as I know.

C++ consts are actually constants. Whereas C's const objects are merely
advisory.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top