Thread: An interesting question ` char str[9] = "deadbeef" `

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    An interesting question ` char str[9] = "deadbeef" `

    The code has a compile-time error
    Code:
    char str[9] ;
    str = "dEAdbeef";
    It complains:
    cannot convert from 'const char [9]' to 'char [9]'cannot convert from 'const char [9]' to 'char [9]'
    However when I modify the code to:
    Code:
    char* str ;
    str = "dEAdbeef";
    It works! The compiler doesn't complain anything! How come assigning a 'const char [9]' to 'char *' can work? I think at least it shall cast away the const-ness of "dEAdbeef", right?
    Last edited by meili100; 04-04-2008 at 11:57 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > str = "dEAdbeef";
    This is an assignment, you cannot assign an array, only a pointer

    char str[] = "dEAdbeef";
    This is initialisation, pointers can also be initialised.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thank you. What I meant is that apparently "dEAdbeef" is const char [9] type, how can you assign a const chat[] to a char *, but can't assign it to a char[9]?

    Note that in my examples, both are talking about an assignment.

    Quote Originally Posted by Salem View Post
    > str = "dEAdbeef";
    This is an assignment, you cannot assign an array, only a pointer

    char str[] = "dEAdbeef";
    This is initialisation, pointers can also be initialised.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What I meant is that apparently "dEAdbeef" is const char [9] type, how can you assign a const chat[] to a char *, but can't assign it to a char[9]?
    The assignment of the const char[9] to the char[9] is simply not allowed as assignment of arrays is not allowed. The assignment from a const char[9] to a char* is allowed as there is a conversion from const char[n] to char*. However, this const to non-const conversion of string literals is deprecated, so you should simply initialise with:
    Code:
    const char* str = "dEAdbeef";
    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

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If the compiler doesn't complain about the second part, you need to turn up your warning level.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you want to assign "dEAdbeef" to your char str[9], use strcpy.

    Of course, you should probably be using strings in C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. char ** and argv memory question
    By simo_mon in forum C Programming
    Replies: 3
    Last Post: 08-17-2008, 08:47 AM
  4. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  5. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM