assign string to chr*

Discussion in 'C++' started by cerr, Jul 29, 2011.

  1. cerr

    cerr Guest

    hi,

    how do i correctly assign a string to a char*?
    If i do it
    char *test;
    test= "BLABLA";
    I get "warning: deprecated conversion from string constant to ‘char*’"
    But strcpy() ot sprintf() doesn't work because there's no room
    allocated yet. So do I actually need to allocate space with calloc()/
    malloc()?
    Thanks,
    Ron
     
    cerr, Jul 29, 2011
    #1
    1. Advertising

  2. cerr

    Ian Collins Guest

    On 07/29/11 02:50 PM, cerr wrote:
    > hi,
    >
    > how do i correctly assign a string to a char*?
    > If i do it
    > char *test;
    > test= "BLABLA";
    > I get "warning: deprecated conversion from string constant to ‘char*’"
    > But strcpy() ot sprintf() doesn't work because there's no room
    > allocated yet. So do I actually need to allocate space with calloc()/
    > malloc()?


    Either use std::string, const char* or char[].

    --
    Ian Collins
     
    Ian Collins, Jul 29, 2011
    #2
    1. Advertising

  3. cerr <> wrote:
    > how do i correctly assign a string to a char*?
    > If i do it
    > char *test;
    > test= "BLABLA";
    > I get "warning: deprecated conversion from string constant to ???char*???"


    Try to read the message. The crucial keyword is "constant".
    (Hint: your 'test' variable isn't.)
     
    Juha Nieminen, Jul 29, 2011
    #3
  4. cerr

    MJ_India Guest

    On Jul 29, 11:50 am, cerr <> wrote:
    > hi,
    >
    > how do i correctly assign a string to a char*?
    > If i do it
    > char *test;
    > test= "BLABLA";
    > I get "warning: deprecated conversion from string constant to ‘char*’"
    > But strcpy() ot sprintf() doesn't work because there's no room
    > allocated yet. So do I actually need to allocate space with calloc()/
    > malloc()?
    > Thanks,
    > Ron


    Are you sure you want _editable_ pointer to char?
    1. If yes, you can avoid malloc/new etc and allocate space on stack or
    use std::string for automatic allocation and free.
    char *test;
    char blabla[] = "BLABLA";
    test= blabla;
    2. If no, replace
    char *test;
    with
    const char *test;
    or
    char const *test;
    as per your preferred coding style.

    -Mohit
     
    MJ_India, Jul 30, 2011
    #4
  5. cerr

    Sasha Daje Guest

    test = (char*)"BLABLA";
     
    Sasha Daje, Jul 30, 2011
    #5
  6. cerr

    Ian Collins Guest

    On 07/31/11 10:11 AM, Sasha Daje wrote:

    Context?

    > test = (char*)"BLABLA";


    No you don't want to do that if test is a char*.

    --
    Ian Collins
     
    Ian Collins, Jul 30, 2011
    #6
  7. cerr

    James Kanze Guest

    On Jul 30, 11:35 pm, Ian Collins <> wrote:
    > On 07/31/11 10:11 AM, Sasha Daje wrote:


    > Context?


    > > test = (char*)"BLABLA";


    > No you don't want to do that if test is a char*.


    It depends on where test is defined. I've recently written a
    lot of code full of const_cast<char*>. I had to interface with
    a third party library written in C, which didn't use const (or
    only rarely used const).

    --
    James Kanze
     
    James Kanze, Aug 7, 2011
    #7
  8. cerr

    Ian Collins Guest

    On 08/ 8/11 05:30 AM, James Kanze wrote:
    > On Jul 30, 11:35 pm, Ian Collins<> wrote:
    >> On 07/31/11 10:11 AM, Sasha Daje wrote:

    >
    >> Context?

    >
    >>> test = (char*)"BLABLA";

    >
    >> No you don't want to do that if test is a char*.

    >
    > It depends on where test is defined. I've recently written a
    > lot of code full of const_cast<char*>. I had to interface with
    > a third party library written in C, which didn't use const (or
    > only rarely used const).


    But you didn't do that with string literals, did you?

    --
    Ian Collins
     
    Ian Collins, Aug 7, 2011
    #8
  9. cerr

    SG Guest

    Am 29.07.2011 04:50, schrieb cerr:
    > how do i correctly assign a string to a char*?
    > If i do it
    > char *test;
    > test= "BLABLA";
    > I get "warning: deprecated conversion from string constant to ‘char*’"
    > But strcpy() ot sprintf() doesn't work because there's no room
    > allocated yet. So do I actually need to allocate space with calloc()/
    > malloc()?
    > Thanks,
    > Ron


    Hello Ron,

    I don't feel like you understand what `test` -- the way you declared it
    -- actually is. This is because you're speaking of assigning a string
    to `char*`. But a variable with a type `char*` does not store strings.
    It stores addresses. So, you can only assign some address to it. While
    it is possible to use a string literal on the right hand side of such an
    assignment, what you really assign to test is an address, in this case,
    the address of the first character.

    I suggest you try to explain your higher level goal instead. Why do you
    want to use a pointer? What for? If this is really what you want:
    Where do you want this pointer to point to?

    If you want to declare a variable that is able to STORE string VALUES
    much like an int variable is able to STORE some integer VALUE or a
    double variable is able to STORE an approximation of a real VALUE,
    `char*` is not the type you want but `std::string` is.

    Nobody likes dealing with dynamic memory for string handling manually.
    Some may say they do but I wouldn't believe them. ;-) Luckily, in C++
    there are nicer alternatives. Consider getting a decent C++ book (like
    Accelerated C++) so you can take advantage of the things that C++ has to
    offer.

    SG
     
    SG, Aug 9, 2011
    #9
    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. crjunk
    Replies:
    2
    Views:
    16,916
    crjunk
    Aug 8, 2003
  2. Andi Twine
    Replies:
    3
    Views:
    571
    Natty Gur
    Jan 13, 2004
  3. =?Utf-8?B?RGFuIE5hc2g=?=

    C# equiv of Chr(13)

    =?Utf-8?B?RGFuIE5hc2g=?=, Nov 1, 2004, in forum: ASP .Net
    Replies:
    12
    Views:
    74,855
    Dudeman3000
    Feb 22, 2010
  4. ProJee
    Replies:
    3
    Views:
    459
    ProJee
    Nov 17, 2004
  5. MattB
    Replies:
    3
    Views:
    566
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN
    Nov 19, 2004
Loading...

Share This Page