LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   g++ typecast complaining too much! (https://www.linuxquestions.org/questions/programming-9/g-typecast-complaining-too-much-179808/)

The_Nerd 05-09-2004 11:13 PM

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:

gcc:(line #):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???

jtshaw 05-10-2004 12:34 AM

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

The_Nerd 05-10-2004 12:12 PM

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!

The_Nerd 05-10-2004 12:34 PM

Basically I don't want the following error:

invalid conversion from `char*' to `unsigned char*'

jtshaw 05-10-2004 01:05 PM

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.

The_Nerd 05-10-2004 04:15 PM

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.

nodger 05-10-2004 07:32 PM

its complaining because the function is defined as taking a char *, but you are giving it a unsigned char *

nodger 05-10-2004 07:45 PM

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.

jtshaw 05-11-2004 08:36 AM

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");
}


jtshaw 05-11-2004 08:40 AM

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

The_Nerd 05-11-2004 10:56 AM

Well.... If it can't be done, I guess I will just try to make less typecasting errors :D

Thanks for all your help guys.

kooch 05-11-2004 12:21 PM

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++?

jtshaw 05-11-2004 06:52 PM

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


All times are GMT -5. The time now is 01:05 PM.