Conversion?

This is a discussion on Conversion? within the C Programming forums, part of the General Programming Boards category; From this code Code: char findext(char data[]) { char value[255]; char *temp = strrchr(data, '.'); strcat(value, temp); return value; } ...

  1. #1
    mart_man
    Guest

    Conversion?

    From this code

    Code:
    char findext(char data[]) {
        char value[255];
        
        char *temp = strrchr(data, '.');
        strcat(value, temp);
        
        return value;
    }
    i get the error invalid conversion from `char*' to `char' on the "return value;" line, why?

    where is there even a conversion?

  2. #2
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    the return type is char, and you're trying to return an array of characters. i'm not positive, but try making it 'char *findext(char date[])'

  3. #3
    mart_man
    Guest
    well that fixed the first one(i think, it compiles i havent used the function yet) but no it doesnt like this

    Code:
    int addext(char data[], char ext[]) {
        strcat(data, ext);
        return data;
    }

  4. #4
    mart_man
    Guest
    sorry for typos, it doesnt like the code now. it says
    cannot convert `char*' to `int*' in return

  5. #5
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59
    Your function returns an int, but you tell it to return a char pointer. Of course it's gonna complain.
    Code:
    char *addext(char data[], char ext[]) {
        strcat(data, ext);
        return data;
    }

  6. #6
    mart_man
    Guest
    thanks

  7. #7
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by MadHatter
    the return type is char, and you're trying to return an array of characters. i'm not positive, but try making it 'char *findext(char date[])'
    Nono!!! Watch out -- you don't want to do this. It will compile but you will have a serious bug!

    The reason why is because the pointer value that you are returning poitns to something that was on the stack. As soon as the function is over you no longer know what is in that location.

    What you're best off doing is either passing a pointer to a pointer and dynamically allocating memory for a string and storing its location to the first derefence instead of one you allocate on the stack (just remember to call delete sometime after the function exits), or you can pass a pointer which points to a large enough string and use that in the function instead of one you allocate on the stack.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char *addext(char data[], char ext[]) {
    What's the point of having a function that does a strcat() ? Why not just call strcat() (or strncat() ) directly?

    @mart_man: In your first post, the function name was findext, which is presumably Find Extension, but you also had some attempts to strcat() data. So, what exactly is that function supposed to be doing?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  4. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM
  5. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM

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