[Solved] What type should be used to loop through an array? [duplicate]


Hmm, interesting case. You want to be able to loop with, let’s just call it hypothetically the largest possible number, and thus wrap-around rather than ever having the comparison return false. Something like this might do the trick:

char arr[SIZE_MAX];
size_t i = 0;
do {
    /* whatever you wanna do here */
    ++i;
}
while (i <= sizeof(arr) && i != 0);

Because a do-while will always perform the first iteration, we can put a condition such that the loop will end when i == 0, and it will only happen upon wrapping back around to 0 rather than on the first iteration. Add another condition for i <= sizeof(arr) to take care of cases where we don’t have wrap-around.

solved What type should be used to loop through an array? [duplicate]