convert from string to char array

Discussion in 'C++' started by Gary Wessle, Nov 8, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    Hi
    I have
    char buffer[128] = "jackson";

    how can I get the same effect by
    string name = "jackson";
    char buffer[128] = name; //will not work

    I tried
    conat_cast<char*> (name.c_str());
    which fails

    thanks
    Gary Wessle, Nov 8, 2006
    #1
    1. Advertising

  2. Gary Wessle wrote:
    > Hi
    > I have
    > char buffer[128] = "jackson";
    >
    > how can I get the same effect by
    > string name = "jackson";
    > char buffer[128] = name; //will not work
    >
    > I tried
    > conat_cast<char*> (name.c_str());
    > which fails
    >
    > thanks


    Assignment doesn't work with c-strings. C strings are a different
    animal from the nice C++ string class. You need to use strcpy, to copy
    data from name.c_str() into your buffer. Look here
    (http://www.cplusplus.com/ref/cstring/) for more info on functions to
    deal with c strings. Also google "c strings" for more info.
    doug turnbull, Nov 8, 2006
    #2
    1. Advertising

  3. Gary Wessle wrote:
    > Hi
    > I have
    > char buffer[128] = "jackson";
    >
    > how can I get the same effect by
    > string name = "jackson";
    > char buffer[128] = name; //will not work
    >
    > I tried
    > conat_cast<char*> (name.c_str());
    > which fails
    >
    > thanks


    Hi,

    c_str() returns a conat char* as you probably know. You should perhaps
    (re-)consider why you need a char buffer[128].

    Sincerely,

    Peter Jansson
    http://www.p-jansson.com/
    http://www.jansson.net/
    Peter Jansson, Nov 8, 2006
    #3
  4. Gary Wessle

    Tim Slattery Guest

    Gary Wessle <> wrote:

    >Hi
    >I have
    >char buffer[128] = "jackson";
    >
    >how can I get the same effect by
    >string name = "jackson";
    >char buffer[128] = name; //will not work


    Sure won't, that tries to assign the address of the string object
    named "name" to the char array named "buffer". Nonsensical, probably
    won't even compile. If it does compile it will probably get you a
    memory protection error of some sort.

    You need to copy the contents from one place to another:

    strcpy(buffer, name.c_str());

    --
    Tim Slattery
    Tim Slattery, Nov 8, 2006
    #4
  5. Gary Wessle

    Default User Guest

    doug turnbull wrote:

    >
    > Gary Wessle wrote:
    > > Hi
    > > I have
    > > char buffer[128] = "jackson";
    > >
    > > how can I get the same effect by
    > > string name = "jackson";
    > > char buffer[128] = name; //will not work
    > >
    > > I tried
    > > conat_cast<char*> (name.c_str());
    > > which fails
    > >
    > > thanks

    >
    > Assignment doesn't work with c-strings. C strings are a different
    > animal from the nice C++ string class. You need to use strcpy, to copy
    > data from name.c_str() into your buffer. Look here
    > (http://www.cplusplus.com/ref/cstring/) for more info on functions to
    > deal with c strings. Also google "c strings" for more info.



    He's not attempting assignment here (which of course doesn't work with
    any array type), but rather initialization.

    The rules for initializing an array of char include (C99):


    [#14] An array of character type may be initialized by a
    character string literal, optionally enclosed in braces.
    Successive characters of the character string literal
    (including the terminating null character if there is room
    or if the array is of unknown size) initialize the elements
    of the array.


    Clearly, the pointer returned by a call to c_str() is not a character
    string literal.




    Brian
    Default User, Nov 8, 2006
    #5
  6. Gary Wessle

    red floyd Guest

    Tim Slattery wrote:
    > Gary Wessle <> wrote:
    >
    >> Hi
    >> I have
    >> char buffer[128] = "jackson";
    >>
    >> how can I get the same effect by
    >> string name = "jackson";
    >> char buffer[128] = name; //will not work

    >
    > Sure won't, that tries to assign the address of the string object
    > named "name" to the char array named "buffer". Nonsensical, probably
    > won't even compile. If it does compile it will probably get you a
    > memory protection error of some sort.
    >
    > You need to copy the contents from one place to another:
    >
    > strcpy(buffer, name.c_str());
    >
    > --
    > Tim Slattery
    >



    Better would be to use a vector<char>.

    vector<char> buffer(name.begin(), name.end());
    buffer.push_back('\0');

    You can now access the contiguous data either as a vector, or if you
    need to pass it to a legacy API expecting a non-const char*, you can
    pass &buffer[0].
    red floyd, Nov 9, 2006
    #6
  7. "Gary Wessle дµÀ£º
    "
    > Hi
    > I have
    > char buffer[128] = "jackson";
    >
    > how can I get the same effect by
    > string name = "jackson";
    > char buffer[128] = name; //will not work
    >

    u can try this :
    strncpy(buffer, name , sizeof(buffer)-1);
    =?gb2312?B?seCzzMDL19M=?=, Nov 9, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. andrea_

    how to convert an int array to a char array

    andrea_, Sep 7, 2005, in forum: C Programming
    Replies:
    2
    Views:
    626
    Flash Gordon
    Sep 7, 2005
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,043
    Ian Collins
    May 9, 2006
  3. Replies:
    3
    Views:
    731
  4. davidb
    Replies:
    0
    Views:
    761
    davidb
    Sep 1, 2006
  5. davidb
    Replies:
    6
    Views:
    1,546
    Default User
    Sep 1, 2006
Loading...

Share This Page