Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pointer arithmetic question.

Reply
Thread Tools

Pointer arithmetic question.

 
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-02-2012
S R <> writes:

> On Oct 31, 4:20Â*am, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
>
> [snip]
>
>>
>> Â* Â* Â*char *r = 0;
>> Â* Â* Â*while (*s++ || !r && (r = s - 1) || (*r++ = *t++));

> ^^^^^
> I think there is a problem with the above. Once we reach the end of
> string s, s is not guaranteed to point to a location containing 0 as
> its value, assuming s as being sent in by the caller of the function
> (in your earlier examples you made ",*s =0" which was the correct).
> The concatenation happens through r but the check *s++ is problematic,
> isn't it?


Yes, you're right. I don't know how that got through the test cases
because I had one to cover that situation -- I had the same bug in the
previous version until I added this test case.

There's another bug as well. Because the middle condition is true but
copies no characters, s is incremented once more it needs to be. We have
to assume that s points somewhere big enough for the concatenated string
so incrementing s along with r is safe provided we don't go too far. If
the target is only just big enough, s will be incremented beyond the
specially permitted "one past the end" position.

Oh well... I don't think I'll offer a fix since the "challenge" of
cramming it all in the condition seems a bit old now.

--
Ben.
 
Reply With Quote
 
 
 
 
Prathamesh Kulkarni
Guest
Posts: n/a
 
      11-02-2012
An attempt for one-line condition for strcat.

char *mystrcat(char *s, char *t)
{
char *r = 0, *temp = s;

while ((*s && s++) || (r == 0 && (r = s) || (*r++ = *t++)))
;
return temp;
}
 
Reply With Quote
 
 
 
 
Prathamesh Kulkarni
Guest
Posts: n/a
 
      11-02-2012
A one-liner attempt for strcat.
http://pastebin.com/LUYcE46F

Regards,
Prathamesh
 
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
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57