I have to step through ONE of the strings in the array.
That is a run time thing, i.e. something which happens when the code has been uploaded to the Arduino and executed.
So I assume a run-time solution is okay.
If it is a proper C string, with a '\0' on the end,
[edit]Doh!
strlen(mystring[2]-1)
should, of course, be
strlen(mystring[2])
Thanks PaulS
[/edit]
gives the number of characters in the string, upto but excluding the terminating '\0' at runtime.
Of course, the program could use that fact when stepping through the string:
for (char *p=mystring[2]; *p != '\0'; p++) {
... look at *p ...
}
If this misses the point, would you explain a bit more what needs to be done?
If it is a proper C string, with a '\0' on the end,
strlen(mystring[2]-1)
gives the number of characters in the string, upto but excluding the terminating '\0' at runtime.
The strlen function does not count the trailing NULL, so the -1 is not required. Not to mention that it is misplaced.
and I copied in the hope of making it easier to see the relationship between the original code, and the propsed alternative, and I forgot to correct it.
I believe (from the comment) the OP probably wanted the number of non-zero characters in the string, the -1 was to allow for the terminating '\0', and strlen(mystring[2]) is what was needed.
The strlen function counts the number of non-NULL values in the array, before the first NULL. It returns that value, not the index of the NULL character.
The only reason to subtract one from the value is to get the index of the last non-NULL character.
The strlen function counts the number of non-NULL values in the array, before the first NULL.
By convention NULL is the value of a pointer.
It is only a convention, but so are words in all human languages.
When we erode the conventional meaning of words we reduce our ability to be precise.
A char array doesn't hold pointers, it holds char values.
The terminating character for a C character string is '\0'
A quick Google search reveals you are both incorrect...
I'll copy the refernces for your convenience...
^ "A byte with all bits set to 0, called the null character, shall exist in the basic execution character set; it is used to terminate a character string literal." — ANSI/ISO 9899:1990 (the ANSI C standard), section 5.2.1
^ "A string is a contiguous sequence of characters terminated by and including the first null character" — ANSI/ISO 9899:1990 (the ANSI C standard), section 7.1.1
^ "A null-terminated byte string, or NTBS, is a character sequence whose highest-addressed element with defined content has the value zero (the terminating null character)" — ISO/IEC 14882 (the ISO C++ standard), section 17.3.2.1.3.1
Before you jump on me about "null" versus "NULL" I will point out that they are, in the English language, the same word. There is no difference in meaning between "null-terminated" and "NULL-terminated".
I think this gets back to the distinction between an array of characters. vs a string. A string is an array of characters that is null terminated. So, the array needs to be sized to hold the NULL. But, the string is the array of characters, so strlen should not (and does not) include the NULL.