Initialising C++ arrays

This is a discussion on Initialising C++ arrays within the C++ Programming forums, part of the General Programming Boards category; Can you tell me how to initialise the new style C++ arrays please. I am trying : array<int, 12> monthdays ...

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

    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
    2,698
    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
    21,456
    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
    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

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

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    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
    2,698
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by jimblumberg View Post
    Note: I am using gcc version 4.5.2.
    Works fine in versions >= 4.6
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,456
    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.
    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

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    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
    96
    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
    2,304
    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
    2,698
    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
    96
    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
    2,698
    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, 07:01 AM
  5. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM

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