When looping through a string using strlen(), should the iterator be of type int or size_t?

Hello. Seeing as strlen's return type is size_t, and not int, should I have the loop's iterator be of type size_t as well? If I don't, I get the following warning message: "warning C4018: '<' : signed/unsigned mismatch".

TL;DR:
1
2
3
4
5
6
7
8
9
char our_string[15] = "Hi there!";

for (int i = 0; i < strlen(our_string); i++)
    our_string[i] = 'X';

// Vs.

for (size_t i = 0; i < strlen(our_string); i++)
    our_string[i] = 'X';

I know it doesn't really matter, but these kind of things bug me.

Thanks in advance,
Hallur A. Atlason
For arrays, std::size_t. For std::string, use std::string::size_type.
closed account (Dy7SLyTq)
i believe size_t is a typedef for an int try replacing int with long
@DTSCode std::size_t is an unsigned integral type large enough to represent an array index and is used by most STL containers and most other things in the standard library. When in doubt, use std::size_t, but prefer a container's specific size_type.
Last edited on
closed account (Dy7SLyTq)
so wait, does that mean its built in?
Last edited on
Topic archived. No new replies allowed.