Strdup

This is a discussion on Strdup within the C++ Programming forums, part of the General Programming Boards category; Hello! Im trying to make work this function that works like the real strdup() function but i get "error: invalid ...

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

    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;
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    790
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    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,699
    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, 09:40 AM
  3. Question to do with strdup()
    By John.H in forum C Programming
    Replies: 9
    Last Post: 01-29-2003, 09: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, 09:08 AM

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