Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Initialising an array

Reply
Thread Tools

Initialising an array

 
 
Albert
Guest
Posts: n/a
 
      01-31-2010
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
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      01-31-2010


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.
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      01-31-2010
On Jan 31, 10:54*am, santosh <santosh....@gmail.com> 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.
 
Reply With Quote
 
Albert
Guest
Posts: n/a
 
      01-31-2010
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
 
Reply With Quote
 
Albert
Guest
Posts: n/a
 
      01-31-2010
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
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      01-31-2010
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.
 
Reply With Quote
 
Albert
Guest
Posts: n/a
 
      01-31-2010
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....y+gift+givers#

 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      01-31-2010
On Jan 31, 6:51*am, Eric Sosman <esos...@ieee-dot-org.invalid> 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?
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-31-2010
On 1/31/2010 1:33 PM, Chad wrote:
> On Jan 31, 6:51 am, Eric Sosman<esos...@ieee-dot-org.invalid> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-31-2010
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[i] = 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) kst- <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initialising an array with pointers into the same array. celephicus C Programming 2 06-07-2010 10:41 AM
Initialising a BOOL array to TRUE ?? Simon L C++ 24 08-02-2008 09:28 PM
trouble initialising array Nick Keighley C Programming 4 06-22-2007 10:03 AM
"Partially elided initialisation" on initialising 2-D array mark.bergman@thales-is.com C Programming 3 12-21-2006 03:23 PM
initialising 2-dim character array Jeremy Targett C Programming 6 07-25-2006 11:31 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57