Go Down

Topic: Sizeof one strings in an 2D array (Read 2666 times) previous topic - next topic

artnode

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:

Code: [Select]
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:

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

I would really appreciate hints and ideas!

Thanks,
Mo

Vambo

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

Code: [Select]
length = strlen(yourStringArray[1]);

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

AWOL

Quote
The sizeof operator only works on data types, not variables


That's not strictly correct:
Code: [Select]

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


will assign the variable "sizeof_myArray" the value 50.
"myArray" is not a datatype.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

gbulmer

#3
Apr 27, 2010, 01:38 am Last Edit: Apr 27, 2010, 07:16 pm by gbulmer Reason: 1
Quote
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.

PaulS

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

gbulmer

Quote
... Not to mention that it is misplaced.


How so?

PaulS

#6
Apr 27, 2010, 07:20 pm Last Edit: Apr 27, 2010, 07:21 pm by PaulS Reason: 1
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?

gbulmer

#7
Apr 27, 2010, 07:35 pm Last Edit: Apr 27, 2010, 07:39 pm by gbulmer Reason: 1
Quote
What does myString[2] - 1 mean, anyway?

Not certain, the posted code was:
Code: [Select]
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.

PaulS

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.

gbulmer

Quote
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

crimony

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


a.k.a "ASCII NUL"

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

Coding Badly

gbulmer & crimony,

A quick Google search reveals you are both incorrect...
http://en.wikipedia.org/wiki/Null_character

I'll copy the refernces for your convenience...

^ "A byte with all bits set to 0, called the [glow]null[/glow] 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 [glow]null[/glow] character" -- ANSI/ISO 9899:1990 (the ANSI C standard), section 7.1.1

^ "A [glow]null[/glow]-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".

Groove

Quote
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!
Per Arduino ad Astra

PaulS

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.

Go Up