Thread: warning: deprecated conversion from string constant to ‘char*

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    269

    warning: deprecated conversion from string constant to ‘char*

    Code:
    char * seq  = "1234567890";
    Why is this happening? This seems like a very normal thing to do.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Should be const char *.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    269
    That works. Also, I found char seq[] = "1234567890"; worked too.

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    The reason is that "1234567890" creates a string constant somewhere in memory. It cannot be modified in any way. So any pointer to it should explicitly be said to be a pointer to a constant.

    char seq[] = "1234567890" is a bit of an abuse of notation; it is shorthand for
    Code:
    char seq[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' }
    which is a perfectly legitimate array initialization.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    i want to say ,my program is all normal

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by TheBigH
    char seq[] = "1234567890" is a bit of an abuse of notation; it is shorthand for
    Actually, it is shorthand for:
    Code:
    char seq[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0' '\0'}
    I don't see why it would be "a bit of an abuse of notation".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    yeah ! #6 is right !

  8. #8
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by laserlight View Post
    Actually, it is shorthand for:
    Code:
    char seq[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0' '\0'}
    I don't see why it would be "a bit of an abuse of notation".
    Whoops, you're right. I forgot the '\0'.

    I call it a bit of an abuse of notation because AFAIK that is the only time in C where something inside quotation marks is handled as shorthand for something else.
    Code:
    while(!asleep) {
       sheep++;
    }

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    IMHO it is not the least bit abuse.
    It's no different to me than using a += b; instead of a = a + b; is. It's just two different ways of representing the same thing.
    In fact the shorter, more concise option is definitely the preferred option.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. warning: deprecated conversion from string constant to char*
    By Albinoswordfish in forum C++ Programming
    Replies: 2
    Last Post: 12-23-2008, 10:24 AM
  2. Replies: 7
    Last Post: 09-24-2008, 03:48 AM
  3. Replies: 5
    Last Post: 04-12-2008, 12:40 PM
  4. 'strcpy' was declared deprecated warning
    By rahulsk1947 in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2007, 09:11 AM
  5. Replies: 5
    Last Post: 10-09-2002, 12:37 PM