Thread: Initializing an arrays of integers

  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,663
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

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, 09: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