470,862 Members | 1,825 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,862 developers. It's quick & easy.

trying to find the error

mdh
K & R 5-5 asks for a strncat function ( concat n characters of t);

void mystrncat(char *s, char *t, int n)

{

while ( *s++); /* find end of s */ /* <<<<< 1 */

/* stops at '\0' */ /* <<<<<2 */

while ( *t && n-- 0)

*s++ = *t++; /* /*<<<<< 3 */

while ( n -- 0);
*s++ = '\0';

}

Now, with 1 I **Thought** that *s++ fails when *s == '\0', so that s
points to '\0'.
So, when 3 occurs, I thought the first char of t ( *t) is assigned to
the "Old" position of s, which should be '\0', but it is not.
What am I missing.
Thanks in advance.

Jun 27 '08 #1
8 997
mdh wrote:
K & R 5-5 asks for a strncat function ( concat n characters of t);

void mystrncat(char *s, char *t, int n)

{

while ( *s++); /* find end of s */ /* <<<<< 1 */

/* stops at '\0' */ /* <<<<<2 */

while ( *t && n-- 0)

*s++ = *t++; /* /*<<<<< 3 */

while ( n -- 0);
*s++ = '\0';

}

Now, with 1 I **Thought** that *s++ fails when *s == '\0', so that s
points to '\0'.
So, when 3 occurs, I thought the first char of t ( *t) is assigned to
the "Old" position of s, which should be '\0', but it is not.
What am I missing.
The loop stops when `s' points at '\0' *before* being
incremented, but the increment happens regardless of the
outcome of the test. After the loop, `s' points not at
the '\0', but at the character immediately after it.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #2
mdh
On May 1, 8:22*pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
, but the increment happens regardless of the
outcome of the test.
Ok...I understand. Thank you.

Jun 27 '08 #3
mdh
* * *The loop stops when `s' points at '\0' *before* being
incremented, but the increment happens regardless of the
outcome of the test.
May I just ask this. I recently had an extensive explanation about
this from one of the members of the group. Is this then a fair
statement about what happens.
When the loop fails, it is still proceeds to the next sequence point.
In other words, it is not like a "break" statement in a loop.
thanks
Jun 27 '08 #4
mdh wrote:
> The loop stops when `s' points at '\0' *before* being
incremented, but the increment happens regardless of the
outcome of the test.

May I just ask this. I recently had an extensive explanation about
this from one of the members of the group. Is this then a fair
statement about what happens.
When the loop fails, it is still proceeds to the next sequence point.
In other words, it is not like a "break" statement in a loop.
The loop does not fail, the test yields false. The expression "s++" is
always evaluated before the result of the expression is tested.

If you want s to point to the end of the string, use

while( *s ) { s++; }

--
Ian Collins.
Jun 27 '08 #5
mdh
On May 1, 9:20*pm, Ian Collins <ian-n...@hotmail.comwrote:
>
The loop does not fail, the test yields false. *The expression "s++" is
always evaluated before the result of the expression is tested.
thank you.

Jun 27 '08 #6
jt
On May 2, 9:39 am, mdh <m...@comcast.netwrote:
On May 1, 9:20 pm, Ian Collins <ian-n...@hotmail.comwrote:
The loop does not fail, the test yields false. The expression "s++" is
always evaluated before the result of the expression is tested.

thank you.
and moreover the unary operators are right to left associative.since
here its post incrementation,s is incremented in the next statement.
probably you can overcome this by using

while(*s)s++;
Jun 27 '08 #7
jt wrote:
On May 2, 9:39 am, mdh <m...@comcast.netwrote:
>On May 1, 9:20 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>The loop does not fail, the test yields false. The expression "s++" is
always evaluated before the result of the expression is tested.
thank you.

and moreover the unary operators are right to left associative.since
here its post incrementation,s is incremented in the next statement.
s++ is the statement, there isn't a next one.
probably you can overcome this by using

while(*s)s++;
Which is exactly what I wrote....

--
Ian Collins.
Jun 27 '08 #8
rio

"mdh" <md**@comcast.netha scritto nel messaggio
news:75**********************************@b5g2000p ri.googlegroups.com...
>K & R 5-5 asks for a strncat function ( concat n characters of t);

void mystrncat(char *s, char *t, int n)

{

while ( *s++); /* find end of s */ /* <<<<< 1 */
if s==0 there is an error
if *s=='\0' there is an error

is it better? "if(s) {while(*s) ++s;}
else return 0;
"
/* stops at '\0' */ /* <<<<<2 */
while ( *t && n-- 0)
*s++ = *t++; /* /*<<<<< 3 */
why do you want the below and not a simple
"if(n-->0) *s=0;
else return error;"
while ( n -- 0);
*s++ = '\0';

}

Now, with 1 I **Thought** that *s++ fails when *s == '\0', so that s
points to '\0'.
this suggest to me that "while ( *s++); " is not the right loop
So, when 3 occurs, I thought the first char of t ( *t) is assigned to
the "Old" position of s, which should be '\0', but it is not.
What am I missing.
Thanks in advance.


Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Jim Patterson | last post: by
6 posts views Thread by Frank Wilson | last post: by
1 post views Thread by Paloma García | last post: by
42 posts views Thread by Martin Jørgensen | last post: by
1 post views Thread by Giuseppe.G. | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.