array init

Discussion in 'C Programming' started by Servé Laurijssen, Oct 29, 2005.

  1. Is it possible for an array to be initialised like this:

    int arr[10] = { 0..9 };

    So that the array holds 0 to 9 after that?
     
    Servé Laurijssen, Oct 29, 2005
    #1
    1. Advertising

  2. Servé Laurijssen

    Michael Mair Guest

    Servé Laurijssen wrote:
    > Is it possible for an array to be initialised like this:
    >
    > int arr[10] = { 0..9 };
    >
    > So that the array holds 0 to 9 after that?


    Nope, at least not that convenient. So, it's back to

    int i;
    int arr[10];
    for (i = 0; i < sizeof arr/sizeof arr[0]; i++)
    arr = i;

    You probably could provide a macro which is replaced by something
    like that but I'd advise against that.
    If you have several lengthy manual initialisations like that, put
    them in a separate functions.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Oct 29, 2005
    #2
    1. Advertising

  3. Servé Laurijssen

    Joe Estock Guest

    Servé Laurijssen wrote:
    > Is it possible for an array to be initialised like this:
    >
    > int arr[10] = { 0..9 };
    >
    > So that the array holds 0 to 9 after that?
    >
    >


    Not in that way. If you omit the size you can.

    int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    The same can be done with any of the basic types.

    -Joe
     
    Joe Estock, Oct 29, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Cheng
    Replies:
    1
    Views:
    8,217
    Juan T. Llibre
    Feb 24, 2006
  2. Victor Hannak
    Replies:
    5
    Views:
    553
    Victor Hannak
    Sep 26, 2003
  3. Replies:
    1
    Views:
    664
    Jules
    Aug 18, 2005
  4. Jess
    Replies:
    4
    Views:
    444
  5. news.aon.at
    Replies:
    11
    Views:
    650
    Ian Collins
    Jan 29, 2011
Loading...

Share This Page