Hi,
I was wondering how strlen is implemented.
What if the input string doesn't have a null terminator, namely the
'\0'?
Thanks a lot
Roy
Nov 14 '05
66 7786
Tim Rentsch wrote: Normally I expect 'for' statements are used when iterating over known quantities; also they usually "do" something with each element iterated over. Of course these conditions needn't be true but most often they are. So the for loop here seems a little off.
On the other hand, 'while' statements are often used to establish postconditions. The code
which is more or less the definition for 'n' being the length of the string 's'. (Initializing 'n' on its declaration is just a convenient shortening of an initializing expression.)
Certainly you're right that operationally the two functions are equivalent. It just seems to be a little more mental effort to be sure that the 'for' code is doing the right thing - it's less clear or less obvious or perhaps both. For these reasons I tend to favor the 'while' form here.
My preferences for while loop vs. for loop, tends to go
according to aesthetics which I don't consider to be
related to style, in the sense that "good programming style"
means enhanced maintainability .
I think that for loops look funny when they have
empty expressions or statements.
I think your originally posted function definition
was only busy enough to fill up a while loop.
while( s[n] ) n++;
My preference for a loop to do something N times,
is a count down while loop.
void do_something_N_ times(unsigned n)
{
while (n-- != 0) {
/* do something */
}
}
Always using a compound statement as a loop body,
*is* something that I consider to be a style issue.
--
pete
Y'know, I'm a bit disappointed no one's proposed the elegant:
static size_t strlen_c(const char *s, size_t c){
return *s? strlen_c(s+1, c+1) : c;
}
size_t strlen(const char *s) {
return strlen_c(s, 0);
}
:-)
If your implementation optimizes tail recursion, this could even
produce reasonable code (for a normal C routine; usually for strlen
the implementor has other, better tricks available). Sometimes I
miss Scheme.
--
Michael Wojcik mi************@ microfocus.com
I will shoue the world one of the grate Wonders of the world in 15
months if Now man mourders me in Dors or out Dors
-- "Lord" Timothy Dexter, _A Pickle for the Knowing Ones_
In article <d5********@new s1.newsguy.com> ,
Michael Wojcik <mw*****@newsgu y.com> wrote: Y'know, I'm a bit disappointed no one's proposed the elegant:
static size_t strlen_c(const char *s, size_t c){ return *s? strlen_c(s+1, c+1) : c; }
size_t strlen(const char *s) { return strlen_c(s, 0); }
Because it's not elegant!
size_t strlen(const char *s)
{
return *s ? strlen(s+1) + 1 : 0;
}
would be elegant, but unfortunately not tail-recursive.
-- Richard
In article <d5**********@p c-news.cogsci.ed. ac.uk>, ri*****@cogsci. ed.ac.uk (Richard Tobin) writes: In article <d5********@new s1.newsguy.com> , Michael Wojcik <mw*****@newsgu y.com> wrote:
Y'know, I'm a bit disappointed no one's proposed the elegant:
static size_t strlen_c(const char *s, size_t c){ return *s? strlen_c(s+1, c+1) : c; }
size_t strlen(const char *s) { return strlen_c(s, 0); }
Because it's not elegant!
size_t strlen(const char *s) { return *s ? strlen(s+1) + 1 : 0; }
would be elegant, but unfortunately not tail-recursive.
"Would be elegant"? Please! It is or (as in your example) is not.
I would be phenomenally wealthy, but unfortunately I don't possess
absurd amounts of money.
On the other hand, my strlen_c is elegant, and strlen is an elegant
wrapper for it. In accordance with the property of distributive
elegance, that makes my entire proposal elegant.
(This is probably sufficiently silly now.)
On a more serious note, I think continuation-passing style is quite
elegant, when done right. What do you have against it?
--
Michael Wojcik mi************@ microfocus.com
Recently, they appeared at the reopening of the Brookdale Library,
luring passersby with the opportunity to be anonymously silly.
Michael Wojcik wrote: In article <d5**********@p c-news.cogsci.ed. ac.uk>, ri*****@cogsci. ed.ac.uk (Richard Tobin) writes: In article <d5********@new s1.newsguy.com> , Michael Wojcik <mw*****@newsgu y.com> wrote:
Y'know, I'm a bit disappointed no one's proposed the elegant:
static size_t strlen_c(const char *s, size_t c){ return *s? strlen_c(s+1, c+1) : c; }
size_t strlen(const char *s) { return strlen_c(s, 0); }
Because it's not elegant!
size_t strlen(const char *s) { return *s ? strlen(s+1) + 1 : 0; }
would be elegant, but unfortunately not tail-recursive.
"Would be elegant"? Please! It is or (as in your example) is not. I would be phenomenally wealthy, but unfortunately I don't possess absurd amounts of money.
On the other hand, my strlen_c is elegant, and strlen is an elegant wrapper for it. In accordance with the property of distributive elegance, that makes my entire proposal elegant.
(This is probably sufficiently silly now.)
On a more serious note, I think continuation-passing style is quite elegant, when done right. What do you have against it?
It seems overly complicated.
size_t strlen(const char *s)
{
return *s ? strlen(s + 1) + 1 : 0;
}
--
pete
pete wrote: Michael Wojcik wrote: In article <d5**********@p c-news.cogsci.ed. ac.uk>, > > ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
size_t strlen(const char *s) { return *s ? strlen(s+1) + 1 : 0; }
size_t strlen(const char *s) { return *s ? strlen(s + 1) + 1 : 0; }
I wasn't paying attention.
--
pete
In article <d5*********@ne ws1.newsguy.com >,
Michael Wojcik <mw*****@newsgu y.com> wrote: On a more serious note, I think continuation-passing style is quite elegant, when done right. What do you have against it?
I think it's wonderful. In fact, I like compilers that translate all
my code into it.
-- Richard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Matt Parkins |
last post by:
Hi,
(I realise this probably isn't precisely the right group for this - could
someone direct me to the appropriate group to post this question? - thanks
!)
I'm using Visual C++ 2005 Express Edition Beta (free download from MS -
hooray!), and everything works fine, except I get warnings back on the use
of some functions, strlen() for example, saying that the function has been
deprecated - although they do still work (which is I guess...
|
by: Matt |
last post by:
I have 2 questions:
1. strlen returns an unsigned (size_t) quantity. Why is an unsigned
value more approprate than a signed value? Why is unsighned value less
appropriate?
2. Would there be any advantage in having strcat and strcpy return a
pointer to the "end" of the destination string rather than returning a
|
by: apropo |
last post by:
what is wrong with this code? someone told me there is a BAD practice with
that strlen in the for loop, but i don't get it exactly. Could anyone
explain me in plain english,please?
char *reverse(char *s)
{
int i;
char *r;
if(!s) return NULL;//ERROR
r=calloc(strlen(s)+1,sizeof(char));
|
by: ¬a\\/b |
last post by:
strlen is wrong because can not report if there is some error
e.g.
char *a;
and "a" point to an array of size=size_t max that has no 0 in it
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |