Thread: Fairly Simple Char Array Question

  1. #1
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294

    Fairly Simple Char Array Question

    I'm not understanding why the string provided for initialization is too long?

    Code:
    int main()
    {
       char array[1] = "A";
    
    
       return 0;
    }
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    C-string are terminated with a null character ('\0'). You don't have enough room in the array for the compiler to include this.

  3. #3
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    So when I have a string in quotes, there is an implied null byte at the end?
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Yes, at least for an initialization of a char array, since that is the definition of a C-string (char array terminated with a null character). However, I'm not a C++ person, so I'm not qualified to speak about C++ strings in general.

    See for yourself:

    Code:
    #include <iostream>
    
    int main()
    {
       char array[2] = "A";
    
       if(array[1] == '\0')
            std::cout << "null char\n";
    
       return 0;
    }
    Last edited by Matticus; 05-09-2014 at 02:08 PM.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Matticus View Post
    Yes, at least for an initialization of a char array, since that is the definition of a C-string (char array terminated with a null character). However, I'm not a C++ person, so I'm not qualified to speak about C++ strings in general.
    String literals in C++ (like "A") are specified in the same way as string literals in C.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,743
    Well, there is a slight difference in that in C, the string literal would not be too long, but rather array would be initialised such that array[0] would have the value of 'A', since the null character implied in the string literal is only used for initialisation if there is sufficient space, unlike in C++.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In other words, this is legal in C:

    char array[1] = "A";

    but not in C++.
    But beware that if it was legal, you no longer have a C-style string, so attempts to use this as a string (using string functions such as strcpy, stcat, etc, or even initializing a std::string with it, e.g. std::string s(array)) will result in undefined behaviour.
    There are ways around it, but the point is be careful. If at all possible, don't do it at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help on a fairly simple code
    By Daniel Camburn in forum C Programming
    Replies: 2
    Last Post: 09-07-2012, 02:07 PM
  2. simple char array question
    By yahn in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2006, 09:18 PM
  3. Fairly simple problem
    By fatdunky in forum C Programming
    Replies: 1
    Last Post: 11-14-2005, 11:34 PM
  4. This should be fairly simple to fix.
    By Lancer in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2004, 08:57 PM
  5. simple char array question..
    By Captain Penguin in forum C++ Programming
    Replies: 6
    Last Post: 10-15-2002, 11:28 AM