Initialising an array

A

Albert

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
 
S

santosh

Albert said:
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.
 
S

santosh

Albert said:
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.
 
A

Albert

santosh said:
Albert wrote:

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
 
S

santosh

Albert said:
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.
 
C

Chad

-----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?
 
E

Eric Sosman

-----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.
 
K

Keith Thompson

Daniel Molina Wegener said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top