const char* to char array.

This is a discussion on const char* to char array. within the C++ Programming forums, part of the General Programming Boards category; I have been searching many places, trying to figure out how to convert a const char to a char array. ...

  1. #1
    deletedforumuser
    Guest

    const char* to char array.

    I have been searching many places, trying to figure out how to convert a const char to a char array.

    Here's the error i get:

    cannot convert from 'const char *' to 'char [5000];

    Is there any ways to convert it?


    Here's the code where you'll find the error.

    char str[5000];
    string All;

    str = All.c_str();

    I'm trying first to convert a char array to a const char.

    If there's anyway to convert a string to a char array, then this is completly possible.

    Thank you , and i hope you can help me.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to use strcpy() - C and C++ do not allow assignment into whole arrays.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    To put anything into a char array, you need to use strcpy.

  4. #4
    deletedforumuser
    Guest
    Got everything to work, thank you guys, and i just learned something new.

    Thank you.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    To be safer (and avoid possible buffer overflows) you should use strncpy()
    Code:
    strncpy( str, All.c_str(), sizeof( str ) );

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Is there a reason you need it inside a char array, buffer, specifically?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Is there a reason you need it inside a char array, buffer, specifically?
    Yeah, that's a good question.
    If you just need a const char* version, the string::c_str() function provides that for you.
    If you need a char* copy that you can write to, copy it to a vector<char>, call vector::reserve() to make it big enough for the new data, and pass &v[0] to any non-C++ aware APIs.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by cpjust View Post
    To be safer (and avoid possible buffer overflows) you should use strncpy()
    Code:
    strncpy( str, All.c_str(), sizeof( str ) );
    . . . and whenever you use strncpy(), you have to add the NULL terminator yourself, in case the string was longer than (or the same length as) the buffer.
    Code:
    strncpy( str, All.c_str(), sizeof( str ) );
    str[sizeof(str) - 1] = 0;
    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.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    If that really is a problem, then perhaps strncat is also safe.

    space[0] = '\0';
    strncat(space, s.c_str(), sizeof space);

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    No, no. You see, strncpy() copies as many characters as it can from the source, including the NULL terminator, up to the maximum that you specify. However, if you try something like this
    Code:
    const char *source = "something";
    char dest[4];
    
    strncpy(dest, source, sizeof dest);
    then dest will contain "some", without a NULL terminator. If you want to create a NULL-terminated string with strncpy(), you need to set the last character to a NULL terminator in case of this.

    Code:
    const char *source = "something";
    char dest[4];
    
    strncpy(dest, source, sizeof dest);
    dest[sizeof dest - 1] = '\0';
    Now dest contains "som\0", as you would expect.
    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.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    Ok, but strncat always writes n characters plus a terminating zero. As long as the third parameter is right you'll never copy wrong. Using your example:

    Code:
    char space[4];
    
    space[0] = '\0';
    strnat(space, "something", sizeof space - 1);
    Of course the result is "som".

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Umm . . . no.
    Code:
    $ cat strncpy.c
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        const char *src = "something";
        char dest[4];
        size_t x;
    
        strncpy(dest, src, 4);
    
        for(x = 0; x < sizeof dest; x ++) {
            printf("'&#37;c' [%d]\n", dest[x], dest[x]);
        }
    
        return 0;
    }
    $ gcc strncpy.c -o strncpy
    $ ./strncpy
    's' [115]
    'o' [111]
    'm' [109]
    'e' [101]
    $
    Voil&#224;. Non-NULL-terminated.

    [edit] In your last example, the result may be "som\0", but that's only because you passed "sizeof space - 1". You're relying on space[3] to be '\0', which you haven't set it to. (Plus you misspelled "strncat". )

    Thinking about it a bit, the most efficient way to do this would be like this.
    Code:
    char space[4];
    
    space[sizeof space - 1] = 0;
    strncpy(space, "something", sizeof space - 1);
    That way the strncpy() never overwrites the last character. Or perhaps . . .
    Code:
    char space[4];
    const char *source = "something";
    size_t len;
    
    len = strlen(source);
    if(len >= sizeof space) {
        space[sizeof space - 1] = 0;
    }
    
    strncpy(space, source, sizeof space - 1);
    [/edit]
    Last edited by dwks; 08-05-2008 at 02:18 PM.
    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.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    We aren't communicating.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
    {
        char space [4];
    
        space[0] = '\0';
        strncat(space, "something", sizeof space - 1);
    
        puts(space);
    
        return 0;
    }
    Verily, the output is expected.

    Actually, your new comments indicate a complete misunderstanding of strncat. strncat by definition appends at most n characters from s2 to s1, and a terminating zero. n in my example is computed to 3. So no, in fact, I am not relying on space[3] to be zero, I'm relying on space[0] to be zero, which is a fairly safe assumption to make in what I posted, and probably lots of well written code. If s2 is shorter than s1, there is no problem, either.

    I don't usually say this but RTFM.
    Last edited by whiteflags; 08-05-2008 at 02:32 PM.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You're right, it's my fault. I guess I assumed, from your original post, that you weren't aware of strncpy()'s behaviour. I wasn't aware that strncat() behaved differently from strncpy(). That's crazy. (But then strncpy() is crazy in the first place . . . .)

    My apologies.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Basic things I need to know
    By maxorator in forum C++ Programming
    Replies: 53
    Last Post: 10-15-2006, 04:39 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

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