ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
If you change your string from unsigned char * to char * it'll get ride of the warning. Of you if case it to a char * in the strcpy line it'll stop complaining. I am not sure why you really need to specify it as unsigned anyway if you are truly using it as an ASCII character string.
Btw, is there a particular reason why you are using g++ to compile this? This code is all standard C. gcc -Wall -o blah blah.c didn't report any warnings to me even with string being set as an unsigned char (but g++ did report the errors).
Thats my problem! It doesn't complain about typecast errors like this in gcc, but when I started using g++ to compile my old programs it complains and won't compile them.
So I just want to know the switch to turn off complaining about typecasting under g++. What is it?
I have looked through all the switches in the gcc manual for cpp code and I can't find anything that'll turn that off. -Wno-conversion would do it if it wasn't an error, but because it is flaged as an error (at least in my version of gcc - 3.3.3) it won't help you.
I can't say I understand why you would ever want to compile regular C code with g++ instead of gcc....(why deal with potential abi issues if you don't have to?) but anyway....
I might sound like a jacka** here, but if you want to fix the error how about you fix your code? Because technically speaking your code is wrong and you should get that error. Keep in mind C++ is a lot stricter about the languages rules that C is.
This complains! Then I say, ok, thats believable... Just let me fix that... so I fix it, and I find that I have another problem... when my function uses a char * pointer instead of a unsigned char * all the chars contained in the array are in the range -128/127!!!! Now this sucks, since I am doing some math on these chars... then I change it to unsigned char * and it complains, then I have to do a whole bunch of typecasting.
The other thing that sucks is that this problem is not for just char, it will happen if I go int to unsigned int, or visa-versa... it is just getting irretating, thats all.
if you want the function to work on unsigned char`s just change the
Code:
void thisFuncDoesNothing(char *str)
to
Code:
void thisFuncDoesNothing(unsigned char *str)
and cast it when calling the function, that way you`ll only have to cast once. You can`t avoid casts when programming in c, especially if you want your programs to be fast. I don`t even think of the data as unsigned or signed/ints/foats/pointers anymore, I just think of it all as raw bytes - its easier that way.
I modified your code a little and made it work. But now that I think of it, I didn't test it with any of the chars from the extended part of the ascii chart (> 127). This code did compile fine and it would allow you to do the math you want to do.
Ya, I wasn't even paying attention to that. Malloc returns a void * these days, so you never need to cast it's return value (at least that is the case in stdlib's malloc).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.