Sizeof one strings in an 2D array

Sorry, this might be a very simple question. But I'm really stuck.

I'm storing an array of string – so I end up with a 2-D array:

char* myStrings[]={"abc", "defg", "hijkl"};

I have to step through ONE of the strings in the array. So I need to get the size of one string:

Length = sizeof(mystring[2]-1; // SHOULD RETURN 4 – BUT DOES NOT WORK

I would really appreciate hints and ideas!

Thanks,
Mo

Won't work. The sizeof operator only works on data types, not variables.
To get the length of a string use strlen; as in

length = strlen(yourStringArray[1]);

Note that length does not include the terminating '\0' char

The sizeof operator only works on data types, not variables

That's not strictly correct:

unsigned char myArray [50];
...
...
int sizeof_myArray = sizeof (myArray);

will assign the variable "sizeof_myArray" the value 50.
"myArray" is not a datatype.

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?

HTH
GB

WARNING - the code is Unverfy'ed and untested.

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.

... Not to mention that it is misplaced.

How so?

I suspect that you meant to subtract 1 from the value returned by strlen, not the value passed to strlen. What does myString[2] - 1 mean, anyway?

What does myString[2] - 1 mean, anyway?

Not certain, the posted code was:

Length = sizeof(mystring[2]-1;

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'

HTH
GB

The terminating character for a C character string is '\0'

a.k.a "ASCII NUL"

so "NULL" is incorrect, but "NUL" is okay.

gbulmer & crimony,

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".

A string is a contiguous sequence of characters terminated by and including the first null character

That's an interesting one, isn't it?
If a string includes the terminator, then "strlen" ought to include it too!

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.