regarding typecasting

V

venkat

Hi,

I am new to c++ programming language. I have written small program ,
where i pass the char string then , i find out length.

find_len( char *str); is the proto type for the function.

I did type casting to give new name to char ,

typedef signed char sint;

Then i replaced the function call with

find_len(sint *str);

when i compiled, i am getting the error saying, invalid conversion
from `sint*' to `char*'

i am not able to understand, what was the problem?. Please help me
out in knowing, why this problem is happening.

Appreciate your help in this regard.

Thanks,
Vikas.
 
K

Kai-Uwe Bux

venkat said:
Hi,

I am new to c++ programming language. I have written small program ,
where i pass the char string then , i find out length.

find_len( char *str); is the proto type for the function.

I did type casting to give new name to char ,

typedef signed char sint;

Here you give a new name to signed char not to char. Even if char is signed
on your implementation, it is still a different type.

Then i replaced the function call with

find_len(sint *str);

when i compiled, i am getting the error saying, invalid conversion
from `sint*' to `char*'

i am not able to understand, what was the problem?. Please help me
out in knowing, why this problem is happening.

There is no implicit conversion from signed char * to char *. Therefore, the
function does not match.



Best

Kai-Uwe Bux
 
V

venkat

Here you give a new name to signed char not to char. Even if char is signed
on your implementation, it is still a different type.





There is no implicit conversion from signed char * to char *. Therefore, the
function does not match.
 
V

venkat

Hi Kai-Uwe,

Thanks for replying.

Does that means, i have char and signed char are different in c++,
which is not the case in C language.


vikas
 
K

Kai-Uwe Bux

venkat said:
Hi Kai-Uwe,

Thanks for replying.

Does that means, i have char and signed char are different in c++,
which is not the case in C language.

a) Please don't top post.

b) In C++, char and signed char are different types even if char is signed.
They are required to both have size 1 and there are conversions between
them. But they are different types.

c) I have no idea what is the case in C, which I never learned.


Best

Kai-Uwe Bux
 
M

Markus Moll

Hi

Kai-Uwe Bux said:
b) In C++, char and signed char are different types even if char is
signed. They are required to both have size 1 and there are conversions
between them. But they are different types.

c) I have no idea what is the case in C, which I never learned.

They are also different types in C, but I think it's not as visible there.

Markus
 
D

Default User

venkat said:
Hi,

I am new to c++ programming language. I have written small program ,
where i pass the char string then , i find out length.

Why are you doing this? Is it a training exercise, or something you
think you need? There is a standard function strlen() that does what
you want.




Brian
 
R

Raymond Martineau

Does that means, i have char and signed char are different in c++, which is not the case in C language.

C++, unlike regular C, has strong type checking and doesn't do all
forms of implcit conversions. Some types now need to match more
precicely than before.

If you need to use multiple types for a given function call, you can
create multple instances of that function with different parameters
(which in turn simply type-cast the parameters for use in the main
function.)
 
J

James Kanze

Does that means, i have char and signed char are different in
c++, which is not the case in C language.

No difference between C and C++ here. "signed char" and "char"
are two different types in both languages, and neither language
has implicit conversions between "signed char*" and "char*".
Do i have to have a
"typedef char sint;" , for making the code to work.

Why do you want a typedef to begin with?
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top