LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-09-2004, 11:13 PM   #1
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
g++ typecast complaining too much!


Ok! I have the following program:
Code:
#include <stdio.h>
#include <string.h>
#include <malloc.h>

int main(int argc, char **argv)
{
	unsigned char *string;

	string=(unsigned char *)malloc(strlen("Testing...")+1);
	if (!string)
	{
		printf("Oh no!");
		exit(1);
	}
	strcpy(string, "Testing...");
	printf("This is a string: %s\n", string);
	free(string);
return 0;
}
However, when I compile, gcc says something like the following:

gccline #):cannot convert unsigned char * to char * for argument 1 to strcpy(char *, char *)

and then it bails!!!

I know there is a command line switch to disable the paranoid typecasting thing... what is it???
 
Old 05-10-2004, 12:34 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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).
 
Old 05-10-2004, 12:12 PM   #3
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
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?

Thanks for your time!
 
Old 05-10-2004, 12:34 PM   #4
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
Basically I don't want the following error:

invalid conversion from `char*' to `unsigned char*'
 
Old 05-10-2004, 01:05 PM   #5
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.

Last edited by jtshaw; 05-10-2004 at 01:17 PM.
 
Old 05-10-2004, 04:15 PM   #6
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
You are right about fixing my code, but technically I am getting quite frusterated. If I do the following for example:

Code:
void thisFuncDoesNothing(char *str)
{
}

int main()
{
    unsigned char *str="Hello!";
    thisFuncDoesNothing(str);
}
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.
 
Old 05-10-2004, 07:32 PM   #7
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
its complaining because the function is defined as taking a char *, but you are giving it a unsigned char *
 
Old 05-10-2004, 07:45 PM   #8
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
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.
 
Old 05-11-2004, 08:36 AM   #9
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
I see what your saying now.

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.

Code:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>

void print8(char *string);

int main(int argc, char **argv)
{
   unsigned char *string;

   string=(unsigned char *)malloc(strlen("Testing...")+1);
   if (!string)
   {
         printf("Oh no!");
         exit(1);
      }
   strcpy((char *) string, "Testing...");
   printf("This is a string: %s\n", string);
   print8((char *)string);
   free(string);
return 0;
}

void print8(char *string)
{
   int i;
   for(i=0;i<8;i++)
   {
      printf("%02x ",string[i]);
   }
   printf("\n");
}
 
Old 05-11-2004, 08:40 AM   #10
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Scratch that, it does appear to work ok.

Code:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>

void print8(unsigned char *string);

int main(int argc, char **argv)
{
   unsigned char *string;

   string=(unsigned char *)malloc(strlen("Testing...")+1);
   if (!string)
   {
         printf("Oh no!");
         exit(1);
      }
   strcpy((char *) string, "Testing...");
   string[0] = 255;
   printf("This is a string: %s\n", string);
   print8(string);
   free(string);
return 0;
}

void print8(unsigned char *string)
{
   int i;
   for(i=0;i<8;i++)
   {
      printf("%02x ",string[i]);
   }
   printf("\n");
}
Result:
This is a string: ˙esting...
ff 65 73 74 69 6e 67 2e
 
Old 05-11-2004, 10:56 AM   #11
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
Well.... If it can't be done, I guess I will just try to make less typecasting errors

Thanks for all your help guys.
 
Old 05-11-2004, 12:21 PM   #12
kooch
Member
 
Registered: Mar 2004
Location: Upstate NY
Distribution: Slackware/YDL
Posts: 77

Rep: Reputation: 15
Just a couple of general notes:

_never_ cast the return of malloc unless you're using a really old C compiler that returns char * where the cast can be necessary.

use stdlib.h instead of malloc.h which is non-standard.

malloc isn't a C++ idiom, is there a particular reason you want to compile a C program as C++?
 
Old 05-11-2004, 06:52 PM   #13
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
TC: Why is knoppix complaining? eantoranz Linux - Networking 1 05-31-2005 10:28 AM
Why is JBoss complaining? eantoranz Programming 0 04-26-2005 09:56 AM
Sendmail complaining about load averages nemesisza Linux - Software 1 09-17-2004 07:32 PM
QT complaining wmeler Linux - Software 0 05-13-2004 08:03 PM
a reccomendation for those complaining about 9.1 being slow Araxis777 Mandriva 5 09-24-2003 06:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration