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

This is a discussion on An interesting question ` char str[9] = "deadbeef" ` within the C++ Programming forums, part of the General Programming Boards category; The code has a compile-time error Code: char str[9] ; str = "dEAdbeef"; It complains: cannot convert from 'const char ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    21,660
    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";
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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,317
    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, 10: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

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