Initializing an arrays of integers

This is a discussion on Initializing an arrays of integers within the C++ Programming forums, part of the General Programming Boards category; Code: int a[10] = { 0 }; This will initialized all elements of a to zero on MS Visual C++ ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Initializing an arrays of integers

    Code:
    int a[10] = { 0 };
    This will initialized all elements of a to zero on MS Visual C++ and Dev-C++ 4.9.9.2. I'd like to know if this is part of the C++ standard or if it's compiler dependent. If possible, could you guys provide me a link where I can read about it? Thanks.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    When you use an initializer list for an array, if you specify fewer values than the array size, the rest of the array will be initialized with 0. Try this:
    Code:
    int a[10]={100};
    cout<<a[9]<<endl;
    I think it must be part of the standard, but I couldn't say for sure.

    See:

    http://msdn.microsoft.com/library/de...m/decla_30.asp

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    It's standard - all remaining initialisers are set to 0, '\0', 0.0 or NULL (as befits the type of thing being 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbee: initializing arrays
    By breaka in forum C Programming
    Replies: 11
    Last Post: 06-12-2006, 12:20 PM
  2. Initializing dynamic arrays
    By cunnus88 in forum C++ Programming
    Replies: 9
    Last Post: 11-21-2005, 08:50 AM
  3. Storing integers from file into arrays???? please help
    By adzza69 in forum C++ Programming
    Replies: 5
    Last Post: 09-11-2004, 12:28 AM
  4. reading integers into arrays from text files
    By c_beginner in forum C Programming
    Replies: 6
    Last Post: 08-05-2004, 11:42 AM
  5. Multipling Integers in Arrays
    By Rvaren in forum C++ Programming
    Replies: 10
    Last Post: 10-14-2002, 03:13 PM

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