LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-12-2005, 10:43 AM   #16
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59

Code:
for( x = 0; x < strlen( the_string ); x++ )
I really hope you guys don't write code like that. The use of strlen() there is very inefficient. For short strings like "abc123" it doesn't really matter, but bad habits die hard. A much better way to control the loop would be:
Code:
for( x = 0; the_string[x]; x++ )
If you don't understand why just ask and I'll be happy to explain.
 
Old 09-12-2005, 11:24 AM   #17
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
A valid point, evaluating the expression strlen() on each loop is bad. My code was only intended as an example, and I felt that strlen() illustrated the point better.
 
Old 09-12-2005, 01:45 PM   #18
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Not to send this thread down a coding style path, but...

Yes, bad habits die hard. I definitely still have some myself. However, I think
Code:
for( x = 0; the_string[x]; x++ )
is another bad habit. It's a bad habit because of exactly what itsme86 did after posting it: offered to explain it. If it's not readily understandable at face value or with sparse comments, then there's a better implementation. I would argue this combination of methods is better:
Code:
char *the_string;
unsigned string_length;
unsigned character_id;

...

string_length = strlen( the_string );
for( character_id = 0; character_id < string_length; character_id++ )
Just my thoughts on the subject.
 
Old 09-12-2005, 03:13 PM   #19
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
I would agree. Just be careful on the chance that your loop might dynamically modify the string: then your length would be off.
 
Old 09-12-2005, 04:55 PM   #20
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Well, this thread has served its initial purpose, so if it gets taken down other, unforeseen paths, that's fine by me.

I do see why calling strlen() each interation would be a bad thing, and actually implemented it front of the loop a la Dark_Helmet's suggestion.

The only thing I don't get is itsme86's idea for the loop control:
Code:
for( x = 0; the_string[x]; x++ )
Clearly I don't understand memory allocation well enough, but I don't see how the_string[x] can be guaranteed to return true until we start accessing elements outside its allocated space.

I'd love to hear the explanation, itsme86.
 
Old 09-12-2005, 06:16 PM   #21
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
A string in C is an array of characters terminated with an ASCII 0. So "hello" in memory looks like 'h', 'e', 'l', 'l', 'o', '\0'.

Characters are really just tiny ints and 0 always marks the end of the string. This is also why to hold a 5-letter string like "hello" you need an array that can hold 6 elements. You need to make room for that '\0' at the end.

I hope that explains it well enough.
 
Old 09-12-2005, 06:55 PM   #22
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Ding. Light bulb goes on.

Not outside of its allocated space, but until we hit the null character(i.e., false).

Of course. Thanks for the explanation.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[C] Temperature conversion Mathiasdm Programming 18 08-24-2005 01:00 PM
Converting sLoPPy cASE to Pretty Case with tr lowpro2k3 Programming 4 04-13-2005 08:13 PM
Why are all my upper case files being shown as lower case?? [Kernel 2.6.9-1.667 FC3] t3gah Fedora 4 03-11-2005 04:09 PM
Lower case to upper case letter sudhasmyle Programming 1 12-03-2004 04:15 AM
case conversion roballen Linux - General 3 02-10-2004 02:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration