CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    Is char s[3] = "abc" legal?

    I tried to compile it in VC++ 6.0, there is a compiler error. But a string could be terminated without '\0', couldn't it? Thanks.

  2. #2
    Join Date
    Aug 2007
    Posts
    858

    Re: Is char s[3] = "abc" legal?

    "abc" is 4 characters. 'a', 'b', 'c', '\0'.

  3. #3
    Join Date
    Jul 2005
    Posts
    1,030

    Re: Is char s[3] = "abc" legal?

    Quote Originally Posted by Speedo View Post
    "abc" is 4 characters. 'a', 'b', 'c', '\0'.
    I wonder if it is possible for "abc" is terminated without '\0'?

  4. #4
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Is char s[3] = "abc" legal?

    Code:
    char s[3] = { 'a','b','c' };
    Is one way of accomplishing that.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  5. #5
    Join Date
    Jul 2005
    Posts
    1,030

    Re: Is char s[3] = "abc" legal?

    Quote Originally Posted by Russco View Post
    Code:
    char s[3] = { 'a','b','c' };
    Is one way of accomplishing that.
    That is cool. Any other way? Thanks.

  6. #6
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    Re: Is char s[3] = "abc" legal?

    Code:
    char s[3];
    memcpy(s, "ABC", 3);
    This is asking for trouble unless you know what you are doing...

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Is char s[3] = "abc" legal?

    No char s[3] = "abc" is not legal since "abc" is a const char* (or at least considered as const).
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  8. #8
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Is char s[3] = "abc" legal?

    Quote Originally Posted by S_M_A View Post
    No char s[3] = "abc" is not legal since "abc" is a const char* (or at least considered as const).
    the problem is not the fact that "abc" is a const literal; you can initialize fixed size char arrays with a const literal; the problem is that the to be initialized array must have sufficient characters to store the literal, including the null terminator;

    for example, "char s[3] = "abc";" does not compile, but "char s[4] = "abc";", "char s[5] = "abc";", ... does compile and it's legal.

  9. #9
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Is char s[3] = "abc" legal?

    Eh, totally messed this up in my head...
    Have lately changed a lot of legacy code due to deprecated warnings about char* being assigned with string literals. Seems like that has gotten to me worse than I thought...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  10. #10
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: Is char s[3] = "abc" legal?

    Quote Originally Posted by LarryChen View Post
    I tried to compile it in VC++ 6.0, there is a compiler error. But a string could be terminated without '\0', couldn't it? Thanks.
    why aren't you just writing:
    Code:
    char s[] = "abc";
    ?

    but if you really don't want to waste that one byte (which is the '\0' character) you can also write like this:

    Code:
    char s[3];
    s[0] = 'a';
    s[1] = 'b';
    s[2] = 'c';
    it's pretty the same way as what Russco said ( char s[3] = { 'a','b','c' }; ) only that by the method I've shown you write a bit more.

  11. #11
    Join Date
    Aug 2007
    Posts
    858

    Re: Is char s[3] = "abc" legal?

    Quote Originally Posted by LarryChen View Post
    I wonder if it is possible for "abc" is terminated without '\0'?
    Sure it's possible. But why would you want to? The concept of c-style strings revolves around them being null-terminated. The only time I can think of when you might even think about doing it is in the internals of a string class. In pretty much any other circumstance, you're just making more work for yourself.

  12. #12
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Is char s[3] = "abc" legal?

    Quote Originally Posted by S_M_A View Post
    Eh, totally messed this up in my head...
    Have lately changed a lot of legacy code due to deprecated warnings about char* being assigned with string literals. Seems like that has gotten to me worse than I thought...
    We've all done that, I still have some legacy code which does that.

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Is char s[3] = "abc" legal?

    Well, in C, this is valid:
    Code:
    char s[3] = "abc";
    being equivalent to:
    Code:
    char s[3] = {'a', 'b', 'c'};
    But I don't consider it advisable since readers may ask if it is valid.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured