Initialize elements in an array

Discussion in 'C Programming' started by Mars, Jan 27, 2005.

  1. Mars

    Mars Guest

    If I want to initialize elements in an array (to a value),
    is using for loops the only method??

    Is there any specific functions to do that??
    Mars, Jan 27, 2005
    #1
    1. Advertising

  2. Mars

    Mars Guest

    mentioned:
    > Mars wrote:
    >
    >>If I want to initialize elements in an array (to a value),
    >>is using for loops the only method??

    >
    >
    > You can use initialiser lists when you define your array e.g.
    >
    > int array[5] = { 1, 2, 3, 4, 5 };
    >


    icic~
    so that's the same as Java~


    >
    >>Is there any specific functions to do that??

    >
    > I often use memset to zero arrays.
    >


    yup~
    thx~~
    Mars, Jan 27, 2005
    #2
    1. Advertising

  3. Mars

    Guest

    Mars wrote:
    > If I want to initialize elements in an array (to a value),
    > is using for loops the only method??


    You can use initialiser lists when you define your array e.g.

    int array[5] = { 1, 2, 3, 4, 5 };

    > Is there any specific functions to do that??

    I often use memset to zero arrays.
    , Jan 28, 2005
    #3
  4. Mars

    Prasad Guest

    To add -
    If you wish to only init few items then you may use :
    int x[5] = {9, 3};
    In the above example, only the first two elements are set to the values
    mentioned, most compilers do initialise the rest of the elements to
    zero.
    Though, not sure what the ANSI "C" standards book says about the init
    of the remaining elements.


    ~ Prasad
    Prasad, Jan 28, 2005
    #4
  5. Mars

    Richard Bos Guest

    "Prasad" <> wrote:

    > To add -


    To add _to what_? Learn to use Google Broken Beta properly, get it to
    provide a proper quotation, or get a real newsreader!

    > If you wish to only init few items then you may use :
    > int x[5] = {9, 3};
    > In the above example, only the first two elements are set to the values
    > mentioned, most compilers do initialise the rest of the elements to
    > zero.


    Not most. All. The Standard requires it.

    Richard
    Richard Bos, Jan 28, 2005
    #5
  6. Mars

    infobahn Guest

    Prasad wrote:
    >
    > To add -
    > If you wish to only init few items then you may use :
    > int x[5] = {9, 3};
    > In the above example, only the first two elements are set to the values
    > mentioned, most compilers do initialise the rest of the elements to
    > zero.


    This is guaranteed.

    > Though, not sure what the ANSI "C" standards book says about the init
    > of the remaining elements.


    In the case of a partially-initialised aggregate or union object,
    "all subobjects that are not initialized explicitly shall be
    initialized implicitly the same as objects that have static
    storage duration." - 6.7.8(19).

    "If an object that has static storage duration is not initialized
    explicitly, then:
    — if it has pointer type, it is initialized to a null pointer;
    — if it has arithmetic type, it is initialized to (positive or
    unsigned) zero;
    — if it is an aggregate, every member is initialized (recursively)
    according to these rules;
    — if it is a union, the first named member is initialized (recursively)
    according to these rules." - 6.7.8(10).
    infobahn, Jan 28, 2005
    #6
    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. hyena
    Replies:
    10
    Views:
    15,369
  2. P
    Replies:
    1
    Views:
    1,147
    Joe Kesselman
    Jul 7, 2006
  3. Replies:
    10
    Views:
    671
  4. Replies:
    3
    Views:
    429
    James Kanze
    Apr 19, 2007
  5. syang8
    Replies:
    4
    Views:
    705
    James Kanze
    Jun 21, 2008
Loading...

Share This Page