c equivalence of const_cast?

B

Bj?rn

I'm trying to convert some small piece of c++ code to c, only to ease
linking it to a third language (I'm not knowledgeable in either c or
c++).

What would be the the c equivalence of:
lpData=const_cast<char*>(Something);

Trying to compile this with gcc 3.2.3 (mingw special 20030504-1)

Thanks!
Björn
 
M

Mike Deskevich

i'm not a c++ guru, but i think you should be able to do:
lpData=(const char*)something;
 
K

Keith Thompson

Mike Deskevich said:
i'm not a c++ guru, but i think you should be able to do:
lpData=(const char*)something;

Actually, I think he wants
lpData = (char*)something;

<OT>
A C++ const_cast is used to cast away constness. C++ has several
flavors of casts that are more restricted than C's "Just convert it,
trust me, I know what I'm doing" cast. (C++ has C-style casts as
well.)
</OT>
 
M

Mike Wahler

Bj?rn said:
I'm trying to convert some small piece of c++ code to c, only to ease
linking it to a third language (I'm not knowledgeable in either c or
c++).

What would be the the c equivalence of:
lpData=const_cast<char*>(Something);

lpData = (char*)Something;

(C doesn't have different 'flavors' of casts as does C++.)

-Mike
 
O

Old Wolf

I'm trying to convert some small piece of c++ code to c, only to ease
linking it to a third language (I'm not knowledgeable in either c or
c++).

What would be the the c equivalence of:
lpData=const_cast<char*>(Something);

lpData = (char *)Something;

Same goes for all the other C++ cast keywords
 
N

Nicolas Pavlidis

I'm trying to convert some small piece of c++ code to c, only to ease
linking it to a third language (I'm not knowledgeable in either c or
c++).

In C doesn't exsist a const keyword, normally, some compiler support
const variables, like gcc, but IMHO it is the best way to remove the
const from the source, and replace it with defines.
What would be the the c equivalence of:
lpData=const_cast<char*>(Something);

If you have done the thing described above just remove the
const_cast<char*> statement, and assign Something to lpData:

lpData = Something;
Trying to compile this with gcc 3.2.3 (mingw special 20030504-1)

Thats because gcc supports (in C - Mode) const but not the C++
const_cast<..>

HTH && Kind regrads
icolas
 
K

Keith Thompson

Nicolas Pavlidis said:
In C doesn't exsist a const keyword, normally, some compiler support
const variables, like gcc, but IMHO it is the best way to remove the
const from the source, and replace it with defines.

C certainly does have a "const" keyword (though it doesn't have
"const_cast").
 
C

CBFalconer

Nicolas said:
In C doesn't exsist a const keyword, normally, some compiler
support const variables, like gcc, but IMHO it is the best way to
remove the const from the source, and replace it with defines.


If you have done the thing described above just remove the
const_cast<char*> statement, and assign Something to lpData:

lpData = Something;


Thats because gcc supports (in C - Mode) const but not the C++
const_cast<..>

Before supplying flawed advice, I suggest you at least read the
other replies to this enquiry, some of which were quite accurate.
 
B

Bj?rn

Thank you for all your suggestions. Coming from Ada language I'm
always quite surprised of how easy it often is to type cast in C, good
or bad.

Björn
 
B

Ben Pfaff

Thank you for all your suggestions. Coming from Ada language I'm
always quite surprised of how easy it often is to type cast in C, good
or bad.

All type casts are explicit in C. I don't see how that's "easy"
or "hard", it's just a matter of typing.
 
N

Nicolas Pavlidis

Keith Thompson said:
C certainly does have a "const" keyword (though it doesn't have
"const_cast").

Maybe I'm wrong, but AFAIK const is not supported by C89, if I'm wrong
tell me! So I recommended to use #defines instead of const becaue
support of C99 is not the best one with some compilers...

Kind regrads,
Nicolas
 
K

Keith Thompson

Ben Pfaff said:
All type casts are explicit in C. I don't see how that's "easy"
or "hard", it's just a matter of typing.

Part of the previous poster's surprise may have to do with a confusion
between casts and conversions. A cast is a syntactic construct that
looks like:
(type_name)expression
and specifies a conversion. There is no such thing as an implicit
cast. A conversion is an operation that happens at run time (or it
might happen during compilation if the optimizer is clever).

It's all too common to misuse the term "cast" or "type cast" to refer
to conversions rather than to the syntactic construct.

<OT>
Compared to Ada, C has far more cases of implicit conversions. In C,
you can convert any numeric type to any other numeric type just by
assigning the value; Ada requires such conversions to be done
explicitly. C also allows explicit conversions in more cases;
conversions between incompatible pointer types (that can lead to
undefined behavior in C) aren't allowed in Ada. Ada does have a type
punning mechanism called Unchecked_Conversion (often needed for
systems-level programming).
</OT>
 
J

Jonathan Adams

Nicolas Pavlidis said:
Maybe I'm wrong, but AFAIK const is not supported by C89, if I'm wrong
tell me! So I recommended to use #defines instead of const becaue
support of C99 is not the best one with some compilers...

Check your K&R 2nd edition. "const" was added as part of ANSI C -- i.e.
C89. It's even mentioned in the "changes from K&R C" appendix.

I think writing new code to be backwards compatible with pre-ANSI C
compilers is a bit silly nowadays.

Cheers,
- jonathan
 
M

Mark McIntyre

All type casts are explicit in C. I don't see how that's "easy"
or "hard", it's just a matter of typing.

compared to C++, where the compiler tends to fight you every step of the
way, in C the compiler tends to treat casts as The Word From The
Mountain.... which is of course what makes them tricky.....
 
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.
 
B

Ben Pfaff

Mark McIntyre said:
You're wrong. It just isn't the same keyword as in C++, which probably
confuses some people.

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.
 
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.
 
C

Chris Torek

Mark McIntyre said:
[const] just isn't the same keyword as in C++, which probably
confuses some people.

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:

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

but valid in C++ (because N is not a constant in C, but is in C++):

% cc -o t t.c
t.c:2: variable-size type declared outside of any function
% g++ -o t++ -x c++ t.c
% ./t++
% echo $status [examines the value returned from main()]
0
%
Contrariwise, the following works as C but not as C++:

/* file1.c */
const int N = 10; /* note lack of "extern" */
int f2(void);
int main(void) {
return f2();
}

/* file2.c -- a separate translation unit */
#include <stdio.h>
#include <stdlib.h>

extern const int N;

int f2(void) {
int i, *p, ret;

printf("N is %d (we assume it is at least 1)\n", N);
p = (int *)malloc(N * sizeof *p); /* cast required for C++ */
p[0] = 42;
for (i = 1; i < N; i++)
p = p[0] * (5 - i);
ret = !p[N - 1];
free(p);
return ret;
}

Here, in C, N is shared across the two translation units, but in
C++ file2.c's extern "N" is missing:

% cc -o c file[12].c
% ./c
N is 10 (we assume it is at least 1)
% echo $status
0
% g++ -o c++ -x c++ file[12].c
/tmp/ccg7oeo1.o: In function `f2(void)':
/tmp/ccg7oeo1.o(.text+0xc): undefined reference to `N'
/tmp/ccg7oeo1.o(.text+0x22): undefined reference to `N'
/tmp/ccg7oeo1.o(.text+0x55): undefined reference to `N'
/tmp/ccg7oeo1.o(.text+0x88): undefined reference to `N'
%
 

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,834
Members
47,382
Latest member
MichaleStr

Latest Threads

Top