问题
Let's have this array:
char arr[SIZE_MAX];
And I want to loop through it (and one past its last element):
char *x;
for (i = 0; i <= sizeof(arr); i++)
x = &arr[i];
(Edited to add some valid code with pointers).
What is the most adequate type for i
?
I accept non-standard types such as POSIX's types.
Related question:
Assign result of sizeof() to ssize_t
回答1:
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.
回答2:
Let's have this array:
char arr[SIZE_MAX];
Note that many C implementations will reject that, SIZE_MAX
being larger than any array they can actually support. SIZE_MAX
is the maximum value of type size_t
, but that's not the same thing as the (implementation-dependent) maximum size of an object.
And I want to loop through it (and one past its last element):
for (i = 0; i <= sizeof(arr); i++)
What is the most adequate type for i?
There is no type available that we can be confident of being able to represent SIZE_MAX + 1
, and supposing that arr
indeed has size SIZE_MAX
, you need i
's type to be able to represent that value in order to avoid looping infinitely. Unless you want to loop infinitely, which is possible, albeit unlikely.
On the other hand, if we have
char arr[NOT_SURE_HOW_LARGE_BUT_LESS_THAN_SIZE_MAX];
then the appropriate type for i
is size_t
. This is the type of the result of the sizeof
operator, and if the only assumption we're making is that the object's size is smaller than SIZE_MAX
then size_t
is the only type we can be confident will serve the purpose.
If we can assume tighter bounds on the object's size, then it may be advantageous to choose a different type. In particular, if we can be confident that it is smaller than UINT_MAX
then unsigned int
may be a slightly more performant choice. Of course, signed types may be used for i
, too, provided that they can in fact represent all the needed values, but this may elicit a warning from some compilers.
回答3:
size_t
is your friend in this case. It guarantees you access to any array index.
来源:https://stackoverflow.com/questions/55303654/what-type-should-be-used-to-loop-through-an-array