View previous topic :: View next topic |
Author |
Message |
chongado
Joined: 25 Apr 2009 Posts: 12
|
confuse about strlen |
Posted: Sun Apr 26, 2009 9:39 pm |
|
|
Hi there, I'm confusing about function strlen()
when I coding like this
char str[10];
str[0]='a';
when I show the result of strlen(str), It'll be 3.
why??? why it didn't be 1?
![Question](images/smiles/icon_question.gif) |
|
![](templates/subSilver/images/spacer.gif) |
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Sun Apr 26, 2009 9:53 pm |
|
|
You did not use any string functions to populate your string. If you want to do it manually then you need to terminate the string with a 0x00.
Code: | char str[10];
str[0]='a';
str[1]=0x00; |
This will give you a string length of 1. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
![](templates/subSilver/images/spacer.gif) |
chongado
Joined: 25 Apr 2009 Posts: 12
|
about terminate character |
Posted: Sun Apr 26, 2009 10:25 pm |
|
|
Thank you. |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Apr 27, 2009 2:07 am |
|
|
char str[10]; defines a char array which can hold upto 10 characters.
A string is series of chars terminated with a 0 NOT the char '0' but the value 0 which is the same as the char '\0' or null
Your code defines a char array and then sets the first value to 'a' the rest is random data and it just so happens that when you ran your code str[3] == '\0' which meant that strlen(str) returns the value 3.
As you have defined your char array as [10] you can only store a tring of upto 9 values for your string as the last char must contain the terminator '\0' or 0 |
|
![](templates/subSilver/images/spacer.gif) |
chongado
Joined: 25 Apr 2009 Posts: 12
|
|
Posted: Mon Apr 27, 2009 3:08 am |
|
|
Wayne_ wrote: | char str[10]; defines a char array which can hold upto 10 characters.
A string is series of chars terminated with a 0 NOT the char '0' but the value 0 which is the same as the char '\0' or null
Your code defines a char array and then sets the first value to 'a' the rest is random data and it just so happens that when you ran your code str[3] == '\0' which meant that strlen(str) returns the value 3.
As you have defined your char array as [10] you can only store a tring of upto 9 values for your string as the last char must contain the terminator '\0' or 0 |
Thank you
You mean I should use the last of array to '/0'
can I code this " str[1]='/0' ", right? |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Apr 27, 2009 4:48 am |
|
|
Make sure you use the correct \
If you only want the one char in your string then you need to do
Code: | str[0] = 'a';
str[1] = '\0'; |
or you could do
which may be better because if you change the number of chars the first
way becomes complicated and is prone to errors.
Code: |
str[0] = 'h';
str[1] = 'e';
str[2] = 'l';
str[3] = 'l';
str[4] = 'o';
str[5] = '\0'; |
same as
Code: | strcpy(str, "hello"); |
|
|
![](templates/subSilver/images/spacer.gif) |
chongado
Joined: 25 Apr 2009 Posts: 12
|
Thank you |
Posted: Mon Apr 27, 2009 11:54 pm |
|
|
Hi There thank you so much I can do it ^^
'\0' lovely character |
|
![](templates/subSilver/images/spacer.gif) |
|