Thread: Initialising C++ arrays

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    101

    Initialising C++ arrays

    Can you tell me how to initialise the new style C++ arrays please.

    I am trying :
    array<int, 12> monthdays (29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    but it doesn't appear to work!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    First you have 13 parameters so your array should be size 13 not 12. Second you need to use braces not the ().
    Code:
    array<int, 13> monthdays = {{29, 31, 28, 31, 30, 31, 30, 31, 31,30, 31, 30, 31}};

    Jim

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can simplify that slightly to:
    Code:
    array<int, 13> monthdays = {29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    Of course, guessing by the name of the array, your initialiser data is not correct
    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

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Thanks Jim

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Quote Originally Posted by laserlight View Post
    You can simplify that slightly to:
    Code:
    array<int, 13> monthdays = {29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    Of course, guessing by the name of the array, your initialiser data is not correct
    I am using monthdays(0) for February in a leap year, and the others equate to monthdays(x) where x is the month number.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Quote Originally Posted by laserlight View Post
    You can simplify that slightly to:
    Code:
    array<int, 13> monthdays = {29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    Of course, guessing by the name of the array, your initialiser data is not correct
    When I tried that I got the following warning:
    main.cpp|6|warning: missing braces around initializer for ‘std::array<int, 13u>::value_type [13]’|
    Note: I am using gcc version 4.5.2.

    Jim

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by jimblumberg View Post
    Note: I am using gcc version 4.5.2.
    Works fine in versions >= 4.6

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jimblumberg
    When I tried that I got the following warning:
    Code:
    main.cpp|6|warning: missing braces around initializer for ‘std::array<int, 13u>::value_type [13]’|
    Note: I am using gcc version 4.5.2.
    Checking the grammar, I'd say that's a compiler bug:
    Quote Originally Posted by C++11 Clause 23.3.2.1 Paragraph 2
    An array is an aggregate that can be initialized with the syntax
    Code:
    array<T, N> a = { initializer-list };
    where initializer-list is a comma-separated list of up to N elements whose types are convertible to T.
    Now, an initializer-list consists of zero or more comma-separated initializer-clauses. An initializer-clause may consist of an assignment-expression. An assignment-expression may consist of an integer constant (integer literal). Therefore, my example is valid, so it was correct for this bug to be fixed in g++ 4.6.
    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

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Okay, that's what I thought. So I guess it's time to upgrade compilers again.

    Jim

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Quote Originally Posted by jimblumberg View Post
    Okay, that's what I thought. So I guess it's time to upgrade compilers again.

    Jim
    I have updated mine to GCC 4.6.1 and I still get a warning if I only use 1 set of braces!!

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I suspect that it's because it thinks you're initializing the internal array of the class, so the outer set of braces is for the class initializer, and the inner set is for the array.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    It looks like there is an open bug report about this issue. See this bug report.

    Jim

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Well spotted Jim!

    I assume then that it is ok just to use 1 set of braces and ignore the warning - or would you recommend using two sets so that there is no warning?

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Since, at the moment, I am not writing programs for anyone but myself I would probably use the two braces to quiet the compiler. However if I were going to distribute a program I would probably not use the initialization list at all, until the bug was resolved. I would manually initialize each element.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initialising multidimensional arrays
    By Fujy in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2010, 08:44 AM
  2. seperating initialising and printing of arrays into functions
    By Tom Bombadil in forum C Programming
    Replies: 1
    Last Post: 03-11-2009, 11:47 AM
  3. Declaring and initialising constant nested arrays
    By officedog in forum C Programming
    Replies: 6
    Last Post: 10-28-2008, 09:55 AM
  4. Initialising Arrays
    By studiesrule in forum C++ Programming
    Replies: 16
    Last Post: 10-31-2006, 08:01 AM
  5. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM