This is a real basic problem. Basically it's telling me that strlen(stamp.country) is not an int. stamp is a struct and country is a c string. I haven't tried putting strlen(stamp.country) into its own variable yet and I'm sure it would work, but I'm just confused as to why this statement is not considered syntactically correct.
Anybody have any clue about this?
1 2 3 4 5
for(int i = 0; i < strlen(stamp.country); i++)
{
buffer[index] = stamp.country[i];
index++;
}
'strlen' returns a value of unsigned integer type. You're trying to compare it to signed integer i. I think the simplest solution would be to explicitly cast the return value of 'strlen' as an int:
for(int i=0; i < (int)strlen(stamp.country); i++) { ... }
Either that or change the type of 'i' to unsigned int (I think that would work...).
Actually the second solution would probably be better (change 'i' to unsigned int). This is because you don't need to deal with negative integers in this case, and if 'strlen' were to return a value greater than a signed int can handle (e.g. a 50,000 character string hehe) then you could run into conversion issues.
also, strlen steps through the entire string EVERY TIME, so this is highly wasteful.
Just call strlen once:
1 2 3
unsigned len = strlen(stamp.country);
for(unsigned i = 0; i < len; ++i)
{ ... }
Or better yet, since you're already stepping through the string with your loop, you don't have to call strlen at all. You can just look for the null yourself: