Initialising an array

Discussion in 'C Programming' started by Albert, Jan 31, 2010.

  1. Albert

    Albert Guest

    I wrote the following line in a program:

    int par[MAX] = { INT_MAX };

    and when I iterated through the array I found that only par[0] is equal
    to INT_MAX (and the rest were zero). Why? Please, if possible, point to
    me to a good section on initialisations *and* arrays in K&R.

    TIA,
    Albert
     
    Albert, Jan 31, 2010
    #1
    1. Advertising

  2. Albert

    santosh Guest

    Albert wrote:
    > I wrote the following line in a program:
    >
    > int par[MAX] = { INT_MAX };
    >
    > and when I iterated through the array I found that only par[0] is equal
    > to INT_MAX (and the rest were zero). Why?


    That's the rule in C if you supply fewer initialisers than the array
    elements.

    > Please, if possible, point to
    > me to a good section on initialisations *and* arrays in K&R.


    See section 4.9 and 8.7 of appendix A.
     
    santosh, Jan 31, 2010
    #2
    1. Advertising

  3. Albert

    santosh Guest

    On Jan 31, 10:54 am, santosh <> wrote:
    > Albert wrote:
    > > I wrote the following line in a program:

    >
    > > int par[MAX] = { INT_MAX };

    >
    > > and when I iterated through the array I found that only par[0] is equal
    > > to INT_MAX (and the rest were zero). Why?

    >
    > That's the rule in C if you supply fewer initialisers than the array
    > elements.
    >
    > > Please, if possible, point to
    > > me to a good section on initialisations *and* arrays in K&R.

    >
    > See section 4.9 and 8.7 of appendix A.


    Clarification: section 4.9 is from the main part of the book, not the
    appendices. Sorry about the potentially ambiguous statement in my
    previous post.
     
    santosh, Jan 31, 2010
    #3
  4. Albert

    Albert Guest

    santosh wrote:
    >
    > Albert wrote:

    <snip>
    >> Please, if possible, point to
    >> me to a good section on initialisations *and* arrays in K&R.

    >
    > See section 4.9 and 8.7 of appendix A.


    There isn't a section 4.9. Section 4 finishes on page 196 on my copy.

    Thanks for the pointer to A8.7, though.

    And BTW, I thought that you had me plonked :|

    Albert
     
    Albert, Jan 31, 2010
    #4
  5. Albert

    Albert Guest

    I wrote:
    <snip>
    > There isn't a section 4.9. Section 4 finishes on page 196 on my copy.

    <snip>

    Sorry, ignore that; I hadn't scrolled down far enough in TB.

    Albert
     
    Albert, Jan 31, 2010
    #5
  6. Albert

    santosh Guest

    Albert wrote:
    > santosh wrote:
    > >
    > > Albert wrote:

    > <snip>
    > >> Please, if possible, point to
    > >> me to a good section on initialisations *and* arrays in K&R.

    > >
    > > See section 4.9 and 8.7 of appendix A.

    >
    > There isn't a section 4.9. Section 4 finishes on page 196 on my copy.
    >
    > Thanks for the pointer to A8.7, though.
    >
    > And BTW, I thought that you had me plonked :|


    Huh!? I don't (and with my current "newsreader," can't), use
    killfiles.
     
    santosh, Jan 31, 2010
    #6
  7. Albert

    Albert Guest

    santosh wrote:
    > Albert wrote:
    >> santosh wrote:
    >>> Albert wrote:

    >> <snip>
    >>>> Please, if possible, point to
    >>>> me to a good section on initialisations *and* arrays in K&R.
    >>> See section 4.9 and 8.7 of appendix A.

    >> There isn't a section 4.9. Section 4 finishes on page 196 on my copy.
    >>
    >> Thanks for the pointer to A8.7, though.
    >>
    >> And BTW, I thought that you had me plonked :|

    >
    > Huh!? I don't (and with my current "newsreader," can't), use
    > killfiles.


    I apologise anyway, in regards to "Message 40" of
    http://groups.google.com/group/comp...befa014538ea548?lnk=gst&q=greedy gift givers#
    :)
     
    Albert, Jan 31, 2010
    #7
  8. Albert

    Chad Guest

    On Jan 31, 6:51 am, Eric Sosman <> wrote:
    > On 1/31/2010 9:38 AM, Daniel Molina Wegener wrote:
    >
    >
    >
    >
    >
    > > -----BEGIN PGP SIGNED MESSAGE-----
    > > Hash: SHA512

    >
    > > On Dom 31 Ene 2010 02:19,
    > > Albert wrote:

    >
    > >> I wrote the following line in a program:

    >
    > >> int par[MAX] = { INT_MAX };

    >
    > >    You must use:

    >
    > >    int par[MAX] = { INT_MAX, INT_MAX, ... MAX times };

    >
    > >> and when I iterated through the array I found that only par[0] is equal
    > >> to INT_MAX (and the rest were zero). Why? Please, if possible, point to
    > >> me to a good section on initialisations *and* arrays in K&R.

    >
    > >    Also you can try the standard function memset:

    >
    > >    int par[MAX];
    > >    memset(par, INT_MAX, MAX);

    >
    >      ... except that this will only work if sizeof(int) == 1.
    > Even if you change the third argument to sizeof(int) * MAX
    > (or to sizeof par), this can work for only a few of the
    > possible values one might want in an int, and only then by
    > lucky (unlucky?) chance.
    >


    Huh? How do you figure?
     
    Chad, Jan 31, 2010
    #8
  9. Albert

    Eric Sosman Guest

    On 1/31/2010 1:33 PM, Chad wrote:
    > On Jan 31, 6:51 am, Eric Sosman<> wrote:
    >> On 1/31/2010 9:38 AM, Daniel Molina Wegener wrote:
    >>
    >>
    >>
    >>
    >>
    >>> -----BEGIN PGP SIGNED MESSAGE-----
    >>> Hash: SHA512

    >>
    >>> On Dom 31 Ene 2010 02:19,
    >>> Albert wrote:

    >>
    >>>> I wrote the following line in a program:

    >>
    >>>> int par[MAX] = { INT_MAX };

    >>
    >>> You must use:

    >>
    >>> int par[MAX] = { INT_MAX, INT_MAX, ... MAX times };

    >>
    >>>> and when I iterated through the array I found that only par[0] is equal
    >>>> to INT_MAX (and the rest were zero). Why? Please, if possible, point to
    >>>> me to a good section on initialisations *and* arrays in K&R.

    >>
    >>> Also you can try the standard function memset:

    >>
    >>> int par[MAX];
    >>> memset(par, INT_MAX, MAX);

    >>
    >> ... except that this will only work if sizeof(int) == 1.
    >> Even if you change the third argument to sizeof(int) * MAX
    >> (or to sizeof par), this can work for only a few of the
    >> possible values one might want in an int, and only then by
    >> lucky (unlucky?) chance.

    >
    > Huh? How do you figure?


    Not sure which part confuses you, so I'll tackle both.

    First, MAX is not the number of bytes in the array unless
    sizeof(int) == 1. So if sizeof(int) > 1 (typical values are
    4 and 2), the given memset() call will store to only part of
    the array and leave the remainder untouched.

    Second, if sizeof(int) > 1 the only values memset() can
    store in an int are those where all the int's bytes are the
    same. For an eight-bit byte and a four-byte int, for example,
    only 0x00000000, 0x01010101, 0x02020202, ..., (int)0xFFFFFFFF
    are achievable by memset(). (It's possible that not even that
    many values are achievable.) In particular, INT_MAX would be
    0x7FFFFFFF, a mixture of the two distinct bytes 0x7F and 0xFF,
    hence not achievable.

    ... and that's how I figure.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jan 31, 2010
    #9
  10. Daniel Molina Wegener <> writes:
    > On Dom 31 Ene 2010 02:19,
    > Albert wrote:
    >
    >> I wrote the following line in a program:
    >>
    >> int par[MAX] = { INT_MAX };

    >
    > You must use:
    >
    > int par[MAX] = { INT_MAX, INT_MAX, ... MAX times };


    Which is reasonable if (and only if) the declaration is generated
    automatically. Suppose you have this:

    #define MAX 3
    int par[MAX] = { INT_MAX, INT_MAX, INT_MAX };

    Then, a few months later, you decide to change MAX to 5:

    #define MAX 5
    int par[MAX] = { INT_MAX, INT_MAX, INT_MAX };

    You're now initializing par to { INT_MAX, INT_MAX, INT_MAX, 0, 0 }.
    If you're lucky, your compiler might warn you about the missing
    initializers, but since it's a perfectly legal initialization it's
    not required to do so.

    Some languages do have constructs for this kind of thing. C doesn't.

    Unless you're generating the code automatically (which is something
    you should consider), your best bet is to use a loop to initialize the
    array:

    #define MAX ...whatever...
    int par[MAX];
    ...
    for (i = 0; i < MAX; i ++) {
    par = INT_MAX;
    }

    [...]

    > Also you can try the standard function memset:
    >
    > int par[MAX];
    > memset(par, INT_MAX, MAX);


    You can try it, but it won't work. memset sets bytes. There is no
    equivalent standard C function that sets ints.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 31, 2010
    #10
    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. Jeremy Targett

    initialising 2-dim character array

    Jeremy Targett, Jul 25, 2006, in forum: C Programming
    Replies:
    6
    Views:
    336
    Keith Thompson
    Jul 26, 2006
  2. Replies:
    3
    Views:
    347
    Al Balmer
    Dec 21, 2006
  3. Nick Keighley

    trouble initialising array

    Nick Keighley, Jun 21, 2007, in forum: C Programming
    Replies:
    4
    Views:
    303
  4. Simon L
    Replies:
    24
    Views:
    1,840
    James Kanze
    Aug 2, 2008
  5. celephicus
    Replies:
    2
    Views:
    291
    Ben Bacarisse
    Jun 7, 2010
Loading...

Share This Page