Pages: [1]   Go Down
Author Topic: Sizeof one strings in an 2D array  (Read 1843 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
Length = sizeof(mystring[2]-1; // SHOULD RETURN 4 – BUT DOES NOT WORK

I would really appreciate hints and ideas!

Thanks,
Mo
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
length = strlen(yourStringArray[1]);

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 236
Posts: 24268
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

That's not strictly correct:
Code:
unsigned char myArray [50];
...
...
int sizeof_myArray = sizeof (myArray);

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

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

UK
Offline Offline
God Member
*****
Karma: 0
Posts: 710
Arduino is Genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: April 27, 2010, 12:16:35 pm by gbulmer » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
God Member
*****
Karma: 0
Posts: 710
Arduino is Genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

How so?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: April 27, 2010, 12:21:08 pm by PaulS » Logged

UK
Offline Offline
God Member
*****
Karma: 0
Posts: 710
Arduino is Genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What does myString[2] - 1 mean, anyway?
Not certain, the posted code was:
Code:
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.
« Last Edit: April 27, 2010, 12:39:18 pm by gbulmer » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
God Member
*****
Karma: 0
Posts: 710
Arduino is Genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 99
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

a.k.a "ASCII NUL"

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12285
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Per Arduino ad Astra

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: