473,399 Members | 3,302 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,399 software developers and data experts.

C++ casting


Hello,

Could someone please explain why the (GCC, ancient 2.95.3 version)
compiler won't let me static_cast<a char* to a unsigned char* or
vice versa? It also won't accept dynamic_cast<for those
conversions. The only one it will permit is reinterpret_cast<>.

BTW, yes I know that pointer casts are 'evil', but here's a for-
example: ostream::write for some reason expects a const char* and a
streamsize parameter, rather than the rather more logical const void*
and a size_t. Many of my applications are mucking around with raw
binary data either in vector<unsigned charor sometimes in raw
unsigned char[], so when I come to write the data out to a file, I
have to cast it to a char*.

Here's a simple example of the problem:

void uc_func(unsigned char *x) {}

void c_func(char *x){}

int main()
{
unsigned char uc[10];
char c[10];

uc_func(uc);
uc_func(static_cast<unsigned char*>(c));

c_func(static_cast<char*>(uc));
c_func(c);
}
---------------------------------
g++ -g -o x x.cc
x.cc: In function `int main()':
x.cc:17: static_cast from `char *' to `unsigned char *'
x.cc:19: static_cast from `unsigned char *' to `char *'
make: *** [xx] Error 1

g++ -g -o x x.cc
xx.cc: In function `int main()':
xx.cc:17: cannot dynamic_cast `c' (of type `char[10]') to type
`unsigned char *'
xx.cc:19: cannot dynamic_cast `uc' (of type `unsigned char[10]') to
type `char *'
make: *** [xx] Error 1

Jul 6 '07 #1
2 2659
ke***@bytebrothers.co.uk wrote:
Could someone please explain why the (GCC, ancient 2.95.3 version)
compiler won't let me static_cast<a char* to a unsigned char* or
vice versa?
Static_cast is only for related types. Char* and unsigned char* are two
unrelated pointer types.
It also won't accept dynamic_cast<for those
conversions.
Why should it? There's no run time information in char, signed char or
unsigned char that would help dynamic_cast. And the types are not class
types in the same inheritance tree.
The only one it will permit is reinterpret_cast<>.
And C-style casts.

--
rbh
Jul 6 '07 #2
On Jul 6, 3:58 pm, k...@bytebrothers.co.uk wrote:
Hello,

Could someone please explain why the (GCC, ancient 2.95.3 version)
compiler won't let me static_cast<a char* to a unsigned char* or
vice versa? It also won't accept dynamic_cast<for those
conversions. The only one it will permit is reinterpret_cast<>.
you cannt use static_cast for casting pointers from one type to
another unless one of them is void* or they are related by
inheritance.

It wont accept dynamic cast either since the two pointers are not
related by inheritance.
BTW, yes I know that pointer casts are 'evil', but here's a for-
example: ostream::write for some reason expects a const char* and a
streamsize parameter, rather than the rather more logical const void*
and a size_t. Many of my applications are mucking around with raw
binary data either in vector<unsigned charor sometimes in raw
unsigned char[], so when I come to write the data out to a file, I
have to cast it to a char*.

Here's a simple example of the problem:

void uc_func(unsigned char *x) {}

void c_func(char *x){}

int main()
{
unsigned char uc[10];
char c[10];

uc_func(uc);
uc_func(static_cast<unsigned char*>(c));

try this: uc_func(static_cast<unsigned char*>(static_cast<void*>(c)));
c_func(static_cast<char*>(uc));
c_func(static_cast<char*>(static_cast<void*>(uc))) ;
c_func(c);}
>
---------------------------------
g++ -g -o x x.cc
x.cc: In function `int main()':
x.cc:17: static_cast from `char *' to `unsigned char *'
x.cc:19: static_cast from `unsigned char *' to `char *'
make: *** [xx] Error 1

g++ -g -o x x.cc
xx.cc: In function `int main()':
xx.cc:17: cannot dynamic_cast `c' (of type `char[10]') to type
`unsigned char *'
xx.cc:19: cannot dynamic_cast `uc' (of type `unsigned char[10]') to
type `char *'
make: *** [xx] Error 1

Jul 6 '07 #3

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

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
2
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.