DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: char*

  1. #1
    Join Date
    May 2004



    I have in my class :-

    private :

    char* m_action;
    char* m_version;
    char* m_message_type;

    I'd like to initialise each one of these member variables to "", i.e. NULL.

    I have :-

    void setAction() { m_action = ""; }

    I'm calling each one of these methods in my c'tor and this seems to work, but I'm getting a warning :-
    Warning: String literal converted to char* in assignment

    Any ideas whether this matters at all ? Am I doing the setting to NULL correctly ?

    Many thanks,


  2. #2
    Join Date
    Dec 2003
    char * can be assigned a constant as sort of a strange historical back door in the language, but you cannot assign a char* to a string apart from creation.

    char * ok = "this is legal"; //works, special case
    char * notok;
    notok = "you cannot do this"; //cannot be done.

    You can do this:
    notok = null;
    ... stuff
    notok = new char[1000];
    sprintf(notok, "you can do this and stuff");

    and so forth.

    You can also make the thing static, initialize it outside the class, and do it that way for a constant string.

    REMEMBER: the string ok is CONSTANT. The string notok can be changed so long as you are careful of you array bounds (max length of the thing).

    Use std::string instead, unless you know what you are doing. The STL strings stink, but char* requires a lot of know-how to use and are vulnerable to hackery and other things.
    Last edited by jonnin; 01-26-2011 at 01:51 PM.

  3. #3
    Join Date
    Oct 2007
    char * ok = "this is legal";
    is *not* legal: neither in C or C++, though many compiler only flag it with a warning. You should use:
    const char* ok = "this really is legal";

    if you must use char* as a member, you must allocate memory for it in the ctor:

    SomeClass() : m_action(new char[MAX]) {
    m_action[0] = '\0';

  4. #4
    Join Date
    Oct 2007
    To be more accurate, I should say that
    char* ok = "deprecated";
    is deprecated. It only exists because very old code has it. I *believe* you risk crashing your program though, because ok[1], may refer to unwritable memory.

  5. #5
    Join Date
    Dec 2003
    right, sorry about forgetting the const, good catch!

  6. #6
    Join Date
    Nov 2003
    if you initialize a char * with a quoted literal, the pointer declaration has to include an explicit const qualifier, indicating that the literal text to which the pointer will point cannot be modified. That's why literals aren't such a great idea when dealing with char, unless they are truly meant to be constant. Either zero initialize the char * (assuming that you will assign a new value to it later) or allocate the memory directly in the initializer using new[].
    Danny Kalev

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.