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"