ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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:
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.