In case you are focred to use a for
-loop and to rely on its condition to break it, you might solve this issue like this:
int main(void)
{
int o[128 + 1] = {-1}, m[256 + 1] = {-1}, s[256 + 1] = {-1};
for(size_t i = 1; /* Use size_t to index arrays */
i < (128 + 1) && o[i-1] != 0 && m[i-1] != 0 && s[i-1] != 0;
i++) {
scanf("%d %d %d", &o[i], &m[i], &s[i]);
}
puts("ok");
}
Please note the values at the arrays' position 0
are not read, but nailed down to -1
.
If your code relies on 0
-based indexing you could trick this the following way:
int main(void)
{
int oa[128 + 1] = {-1}, ma[256 + 1] = {-1}, sa[256 + 1] = {-1};
int *o = oa + 1, *m = ma + 1, *s = sa + 1;
for(size_t i = 0; /* Use size_t to index arrays */
i < 128 && o[i-1] != 0 && m[i-1] != 0 && s[i-1] != 0;
i++) {
scanf("%d %d %d", &o[i], &m[i], &s[i]);
}
puts("ok");
}
A much nicer solution however would be:
int main(void)
{
int o[128], m[256], s[256];
{
ssize_t i = -1; /* If ssize_t isn't available use any signed integer wide enough. */
do {
++i;
scanf("%d %d %d", &o[i], &m[i], &s[i]);
} while (i < 128 && o[i] != 0 && m[i] != 0 && s[i]);
}
puts("ok");
}