Thread: Array Initialization

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    Array Initialization

    the code ->

    int a[10]={0};

    initializes all elements of a to 0, but when i do this ->

    int a[10]={0,1}

    the contents of the array are as follows : 0 1 0 0 0 0 0 0 0 0

    how??

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    How? Up to the compiler.

    The question you probably intended to ask is better expressed as "Why?". The answer to that is : because the standard says so. Essentially, in the declaration
    Code:
    int a[10] = {0, 1};
    the first two elements are initialised as 0 and 1 respectively. For auto arrays (eg local to a function, without the static qualifier) the remaining elements are initialised as they would have been if the array was static. And, if the array was static, those elements would be initialised to zero.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What else were you expecting?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array initialization with int variable
    By tsantana in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2009, 02:48 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Array initialization has me dumbfounded.
    By chad_crider in forum C++ Programming
    Replies: 5
    Last Post: 02-23-2006, 11:13 PM
  4. Difference between array initialization
    By 3saul in forum C Programming
    Replies: 2
    Last Post: 02-05-2006, 11:51 PM
  5. Basic Array Initialization in C
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-18-2002, 06:41 PM