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

This is a discussion on invalid conversion from `unsigned char*' to `char*' within the C Programming forums, part of the General Programming Boards category; hello everyone, I have downloaded "PicUSB" for Linux, which is a program that communicates with PIC18F2550 through USB (sends to ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    2

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

    hello everyone,

    I have downloaded "PicUSB" for Linux, which is a program that communicates with PIC18F2550 through USB (sends to the pic two numbers, waits for the result and shows it in a box). I've compiled it and it works fine, but now i'm trying to use the picusb.c and picusb.h files in another program, and it gives me the error:

    PicUSB.cpp: In function ‘int SumaDosNumerosPIC(char, char)’:
    PicUSB.cpp:143: error: conversión inválida de ‘unsigned char*’ a ‘char*’
    Here's the code:

    Code:
    int SumaDosNumerosPIC (char num1, char num2)
    {
    
       char *bytes;
       char *suma;
       
        unsigned char Estos_Bytes[3];
       bytes =&Estos_Bytes[0];
       suma = bytes;
       
        Estos_Bytes[0]=0x00;
        Estos_Bytes[1]=num1;
        Estos_Bytes[2]=num2;
    return ( (int) *suma);
       
    }
    It's the same file used in the original program, i haven't changed it..

    Thanks in advance (and sorry for my mistakes, i'm a spanish- speaker)

    Picusb for linux: http://webs.ono.com/ma4826/

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Well for me it compiles if I cast what you are setting to bytes as a char pointer. As in:
    Code:
    bytes =(char*)&Estos_Bytes[0];
    I'm not 100% sure if thats a good thing to be doing, but as far as I can see its ok

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If that's the original code, verbatim, then the programmer (you?) must have been having a bad day. Here's some equivalent code:
    Code:
    int SumaDosNumerosPIC (char num1, char num2)
    {
        return 0;
    }
    Presumably you or the original author meant to do something else . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    2
    No i'm not the author, but i don't think this
    Code:
    return ( (int) *suma);
    is an error because compiling the picusb, it works.. the problem i have is when i try to use the same files including them in another program. the compiler says i have an error on this line:
    Code:
    suma = bytes;
    (unsigned char* to char*)

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well, take mike_g's suggestion and try
    Code:
    suma = (char *)bytes;
    But consider this. That function always returns 0. I can prove it. Run this program, which passes every possible value to the function.
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int SumaDosNumerosPIC (char num1, char num2)
    {
    
       char *bytes;
       char *suma;
       
        unsigned char Estos_Bytes[3];
       bytes =&Estos_Bytes[0];
       suma = bytes;
       
        Estos_Bytes[0]=0x00;
        Estos_Bytes[1]=num1;
        Estos_Bytes[2]=num2;
    return ( (int) *suma);
    
    }
    
    int main() {
        int x, y;
    
        for(x = CHAR_MIN; x <= CHAR_MAX; x ++) {
            for(y = CHAR_MIN; y <= CHAR_MAX; y ++) {
                if(SumaDosNumerosPIC(x, y) != 0) {
                    printf("(&#37;i,%i) != 0\n", x, y);
                }
            }
        }
        
        printf("I rest my case.\n");
    
        return 0;
    }
    The only output is
    Code:
    I rest my case.
    which means that the function always returns 0.

    The output is the same with or without casts, I should imagine.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Nicely done.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > PicUSB.cpp: In function ‘int SumaDosNumerosPIC(char, char)’:
    You wrote a C++ program, and posted in C.

    Think about which language you're really wanting to use. Just because it mostly compiles doesn't make it any good.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  2. invalid char to char conversion?
    By kryptkat in forum Windows Programming
    Replies: 2
    Last Post: 09-27-2007, 05:16 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 10:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21