Thread: Strdup

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    Strdup

    Hello!

    Im trying to make work this function that works like the real strdup() function but i get
    "error: invalid conversion from `void*' to `char*'" on the return; line.

    Code:
    #include <iostream>
    using namespace std;
    
    char * strdup(const char * s)
    {
        size_t len = 1+strlen(s);
        char *p = (char*)malloc(len);
    
        return p ? memcpy(p, s, len) : NULL; // error here
    }
    
    int main(int nArg, char* pszArgs[])
    {
        char buf[6] = "hello";
    
        cout << strdup(buf) << endl;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you're using iostream, then I guess you really are writing in C++. C++ has no conversion from void* to char*, unless you explicitly do so with a cast.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thanks Tabstop but i dont see why is it talking about void* to char* conversion when p* and s* are both of type char*.

    Edit: ok i see that its memcopy() that takes void* parameters, so i guess i should use another function.
    Last edited by Ducky; 04-01-2010 at 12:31 PM.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The return type of memcpy is void*. That is what your last line attempts to return, hence the error. If you want that to compile:
    Code:
    return p ? reinterpret_cast<char*>(memcpy(p, s, len)) : NULL;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Ducky View Post
    Hello!

    Im trying to make work this function that works like the real strdup() function but i get
    "error: invalid conversion from `void*' to `char*'" on the return; line.

    Code:
    #include <iostream>
    using namespace std;
    
    char * strdup(const char * s)
    {
        size_t len = 1+strlen(s);
        char *p = (char*)malloc(len);
    
        return p ? memcpy(p, s, len) : NULL; // error here
    }
    
    int main(int nArg, char* pszArgs[])
    {
        char buf[6] = "hello";
    
        cout << strdup(buf) << endl;
    }
    You're using bad C programming practices to write C++ programs. Use an std::string and be done with it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regarding 'strdup()'
    By subhashish1213 in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2009, 09:18 AM
  2. Replies: 15
    Last Post: 11-11-2007, 10:40 AM
  3. Question to do with strdup()
    By John.H in forum C Programming
    Replies: 9
    Last Post: 01-29-2003, 10:30 AM
  4. strdup
    By hankspears in forum C Programming
    Replies: 4
    Last Post: 05-09-2002, 02:02 PM
  5. function: strdup()
    By cjtotheg in forum C Programming
    Replies: 3
    Last Post: 02-02-2002, 10:08 AM