Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > trying to find the error

Reply
Thread Tools

trying to find the error

 
 
mdh
Guest
Posts: n/a
 
      05-02-2008
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.

 
Reply With Quote
 
 
 
 
mdh
Guest
Posts: n/a
 
      05-02-2008
On May 1, 8:22*pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
, but the increment happens regardless of the
> outcome of the test.


Ok...I understand. Thank you.

 
Reply With Quote
 
 
 
 
mdh
Guest
Posts: n/a
 
      05-02-2008

> * * *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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-02-2008
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.
 
Reply With Quote
 
mdh
Guest
Posts: n/a
 
      05-02-2008
On May 1, 9:20*pm, Ian Collins <ian-n...@hotmail.com> wrote:

>
> 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.

 
Reply With Quote
 
jt
Guest
Posts: n/a
 
      05-03-2008
On May 2, 9:39 am, mdh <m...@comcast.net> wrote:
> On May 1, 9:20 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>
>
>
> > 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++;
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-03-2008
jt wrote:
> On May 2, 9:39 am, mdh <m...@comcast.net> wrote:
>> On May 1, 9:20 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>>
>>
>>
>>> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 3 02-14-2011 05:28 AM
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 0 02-10-2011 05:18 PM
Can Not find bean in scope error when trying to display in jsp Rudi Java 4 08-13-2008 07:19 PM
Find.find does not find orphaned links? Wybo Dekker Ruby 1 11-15-2005 02:50 PM
trying to find source of error Bruce D ASP .Net 3 10-30-2004 09:12 AM



Advertisments