473,410 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

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 1101
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jim Patterson | last post by:
I am trying to compile php with sablotron and I get this error message ===================================================================== FAILED TEST SUMMARY...
6
by: Frank Wilson | last post by:
Tom, It sounds to me like ASP, not ASP.NET is handling the request for WebForm1.aspx. This is most likely an IIS config issue that may have been caused by order of installation or...
5
by: tshad | last post by:
I am trying to access my log files and am running into a permissions problem. I am doing the following: ****************************************************************** private void...
1
by: Paloma García | last post by:
Dear all, I have created personalized configuration sections in my web project following the instructions described in this page...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
42
by: Martin Jørgensen | last post by:
Hi, I'm trying to move a matlab program into c language. For those who knows matlab, this is the line I want to program in c: hx(1:nx,1:ny) = 0; % nx=10, ny=10 It works on a 2-dimensional...
3
by: LRI41 | last post by:
I googled an error message and it came up with two posts on your forum and when I got there it said I needed to register to post my question, I am not sure whether is this the correct place to...
1
by: tcc.se7en | last post by:
I was having a bit of an issue with assembly references, but now it seems to have spiraled even further out of control. Not sure what I did to break it, any help is appreciated. Basically, I...
0
by: rbukkara | last post by:
Hi, I have got the following error while trying to add a user in the LDAP Directory. javax.naming.NameNotFoundException: ; remaining name 'uid=vassila,ou=People,dc=cs,dc=uno,dc=edu' I have...
1
by: Giuseppe.G. | last post by:
Hello Paavo, thanks for your kind reply. You may certainly be right, since it appears the author last worked on this toolkit back in 2006. I wish I knew more about templates, unfortunately I'm just...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
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,...
0
isladogs
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...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.