The size_t
type is meant to specify the size of something so it's natural to use it, for example, getting the length of a string and then processing each character:
for (size_t i = 0; i < strlen (str); i++)
str[i]++;
You do have to watch out for boundary conditions of course, since it's an unsigned type. The boundary at the top end is not usually that important since the maximum is usually large (though it is possible to get there). Most people just use an int
for that sort of thing because they rarely have structures or arrays that get big enough to exceed the capacity of that int
.
But watch out for things like:
for (size_t i = strlen (str) - 1; i >= 0; i--)
which will cause an infinite loop due to the wrapping behavior of unsigned values (although I've seen compilers warn against this).