1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

About Arrays !!

Discussion in 'C' started by Dimesh, Jul 12, 2010.

  1. Dimesh

    Dimesh New Member

    Joined:
    Jul 12, 2010
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    What is the difference Between those types of initializing An array called "IntegerArray" With 5 element :
    Code:
         int IntegerArray[5] = {0};
         int IntegerArray[5] = {0,0,0,0,0};
         int IntegerArray[5] = {10,20,30,40,50};
         int IntegerArray[]  = {10,20,30,40,50};
    Please i need an obvious explanation for each line as i am confused.....

    Thanks for reading...............
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    Code:
         int IntegerArray[5] = {0};
    Declares array of integers with each initialized to 0. Basically short form of second one.
    Code:
         int IntegerArray[5] = {0,0,0,0,0};
    Declares array of integers with each initialized to 0.
    Code:
         int IntegerArray[5] = {10,20,30,40,50};
    Declares array of integers with each initialized to respective values.
    Code:
         int IntegerArray[]  = {10,20,30,40,50};
    Declares array of integers with each initialized to respective values. This is again same as above and one more way to write it.
     
  3. Dimesh

    Dimesh New Member

    Joined:
    Jul 12, 2010
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Thanks shabbir, that is helpful ....
     
  4. Dimesh

    Dimesh New Member

    Joined:
    Jul 12, 2010
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Good explanation more than others ....!
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    The pleasure is all mine.
     
  6. LordN3mrod

    LordN3mrod New Member

    Joined:
    Sep 4, 2010
    Messages:
    22
    Likes Received:
    11
    Trophy Points:
    0
    Occupation:
    Software Developer
    Location:
    Yerevan, Armenia
    By all means the explanation of shabbir was excellent, indeed. Just a little complementation from me:
    when you write
    int a[n] = {a[1], a[2] ..., a[t]}
    then,
    if n is greater than t, then the remaining values are set to 0. t is greater than n, this is a compile error.
    if you don't specify n, then n = t;
    sorry for a bit dry explanation, but I'm sure it won't cause trouble :)
     
    shabbir likes this.

Share This Page