CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

confuse about strlen

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
chongado



Joined: 25 Apr 2009
Posts: 12

View user's profile Send private message MSN Messenger

confuse about strlen
PostPosted: Sun Apr 26, 2009 9:39 pm     Reply with quote

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
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sun Apr 26, 2009 9:53 pm     Reply with quote

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



Joined: 25 Apr 2009
Posts: 12

View user's profile Send private message MSN Messenger

about terminate character
PostPosted: Sun Apr 26, 2009 10:25 pm     Reply with quote

Thank you.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Apr 27, 2009 2:07 am     Reply with quote

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
chongado



Joined: 25 Apr 2009
Posts: 12

View user's profile Send private message MSN Messenger

PostPosted: Mon Apr 27, 2009 3:08 am     Reply with quote

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?
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Apr 27, 2009 4:48 am     Reply with quote

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
Code:
strcpy(str, "a");

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");
chongado



Joined: 25 Apr 2009
Posts: 12

View user's profile Send private message MSN Messenger

Thank you
PostPosted: Mon Apr 27, 2009 11:54 pm     Reply with quote

Hi There thank you so much I can do it ^^

'\0' lovely character
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group