char *

This is a discussion on char * within the C++ Programming forums, part of the General Programming Boards category; How would you assign the value of a string variable to a char *?...

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    char *

    How would you assign the value of a string variable to a char *?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    string str;
    char *ptr;

    ptr = str.c_str();
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    tried that...

    I tried that but my compiler gives me the error:

    assignment to `char *' from `const char *' discards qualifiers


    ???

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    That means "You must explicitly cast this".

    ptr = (char*)str.c_str();



    ITSA
    Socket Library!

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    That means "You must explicitly cast this".

    ptr = (char*)str.c_str();
    Preferably with something more informative:
    Code:
    ptr = const_cast<char *> ( str.c_str() );
    -Prelude
    My best code is written with the delete key.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    Bah, you can keep your const_cast, Long Live C!!

    Honestly, though, does it really matter?



    ITSA
    Socket Library!

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Honestly, though, does it really matter?
    Quick, what is this cast doing?

    BLOCK *bblock = (BLOCK *)ablock;

    Is it casting an object of a different type to BLOCK? Is it casting away const so that the assignment is legal? Is it trying to assign a pointer to a derived object to a pointer to the base object? Can you be sure of your answer? How about now?

    BLOCK *bblock = const_cast<BLOCK *> ( ablock );

    Amazing, instant self documenting code.

    -Prelude
    My best code is written with the delete key.

  8. #8
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Argh. Never, never, never cast a const to non-const.

    If string.c_str() returns a const char* this means you are NOT to modify whatever hides behind this pointer. So you should not cast it into something that can be modified. The problem is not your cast. The problem is your usage of char*. Why do you need this to be a char* ? I bet you could use a const char* instead.

    If you post some code I can try to give an example. I have yet to see a situation where the const had to be removed for the program to work properly.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  9. #9
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Originally posted by Prelude
    >Honestly, though, does it really matter?
    Quick, what is this cast doing?

    BLOCK *bblock = (BLOCK *)ablock;

    Is it casting an object of a different type to BLOCK? Is it casting away const so that the assignment is legal? Is it trying to assign a pointer to a derived object to a pointer to the base object? Can you be sure of your answer? How about now?

    BLOCK *bblock = const_cast<BLOCK *> ( ablock );

    Amazing, instant self documenting code.

    -Prelude
    uhh..how about just adding a comment?
    Code:
    ptr = (char*)str.c_str(); // const cast this variable i says!

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    How about doing it right in the first place ?

    const char* cptr = NULL;

    cptr = yourstringvar.c_str();

    // No cast needed
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    Well that is certainly true nvoigt, almost 100% of the time, you should respect a const. I really should have stated that in my first post.



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 10:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

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