Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > why do strcpy, strcat, & co return a pointer ?

Reply
Thread Tools

why do strcpy, strcat, & co return a pointer ?

 
 
Jarmo
Guest
Posts: n/a
 
      11-22-2003
"Nicolas" <> wrote in message
news:3fbfeb99$0$27046$...
> On most implementations of the standard C library, the functions that
> copy characters in a previously allocated buffer, like strcpy or strcat,
> are returning the pointer to that buffer.
>
> On my NetBSD system, man strcpy gives the following prototype :
> char *strcpy(char * restrict dst, const char * restrict src);
>
> What's the point in returning a pointer we already know before the call?
>
> Thank you in advance for an explanation.


So that you can write (unnecessarily abbreviated) code like this:

strcat(path, strcpy(file, "fred.txt"));


 
Reply With Quote
 
 
 
 
Nicolas
Guest
Posts: n/a
 
      11-23-2003
On most implementations of the standard C library, the functions that
copy characters in a previously allocated buffer, like strcpy or strcat,
are returning the pointer to that buffer.

On my NetBSD system, man strcpy gives the following prototype :
char *strcpy(char * restrict dst, const char * restrict src);

What's the point in returning a pointer we already know before the call?

Thank you in advance for an explanation.

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      11-23-2003

"Nicolas" <> wrote in message
news:3fbfeb99$0$27046$...
> On most implementations of the standard C library, the functions that
> copy characters in a previously allocated buffer, like strcpy or strcat,
> are returning the pointer to that buffer.
>
> On my NetBSD system, man strcpy gives the following prototype :
> char *strcpy(char * restrict dst, const char * restrict src);
>
> What's the point in returning a pointer we already know before the call?


Convenience. The result of one of these function calls might
be used as part of a larger expression, e.g.:

printf("%s\n", strcat(s1, s2));

If you don't need the return value, just ignore it.

strcat(s1, s2);

-Mike


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      11-23-2003
Nicolas wrote:
>
> On most implementations of the standard C library, the functions
> that copy characters in a previously allocated buffer, like strcpy
> or strcat, are returning the pointer to that buffer.
>
> On my NetBSD system, man strcpy gives the following prototype :
> char *strcpy(char * restrict dst, const char * restrict src);
>
> What's the point in returning a pointer we already know before the
> call?


It allows you to be baffled by the peculiar run-time actions of:

char p[100] = "Fred";
....
printf("%s or %s\n", p, strcat(p, " and George"));

For improved semantics look up the (non-standard) strlcat and
strlcpy functions.

--
Chuck F () ()
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      11-23-2003
Nicolas wrote:
> On most implementations of the standard C library, the functions that
> copy characters in a previously allocated buffer, like strcpy or strcat,
> are returning the pointer to that buffer.


On ALL implementations. If an implementation were to do something
different, it would no longer be an implementation of the *standard* C
library.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      11-23-2003
CBFalconer <> writes:

> It allows you to be baffled by the peculiar run-time actions of:
>
> char p[100] = "Fred";
> ....
> printf("%s or %s\n", p, strcat(p, " and George"));


That code has undefined behavior, you know.
--
"The expression isn't unclear *at all* and only an expert could actually
have doubts about it"
--Dan Pop
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      11-23-2003
Nicolas wrote:

> Jarmo wrote:
>> "Nicolas" <> wrote in message
>> news:3fbfeb99$0$27046$...
>>
>>>On most implementations of the standard C library, the functions that
>>>copy characters in a previously allocated buffer, like strcpy or strcat,
>>>are returning the pointer to that buffer.
>>>
>>>On my NetBSD system, man strcpy gives the following prototype :
>>>char *strcpy(char * restrict dst, const char * restrict src);
>>>
>>>What's the point in returning a pointer we already know before the call?
>>>
>>>Thank you in advance for an explanation.

>>
>>
>> So that you can write (unnecessarily abbreviated) code like this:
>>
>> strcat(path, strcpy(file, "fred.txt"));

>
> Lol, of course this is obvious.
> That's because I never (well almost) enclose a call in another call.


It's probably wise to never (well almost) enclose a call in another call.
Many functions return error indicators which can be missed by such an
approach.

--
Richard Heathfield :
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
Nicolas
Guest
Posts: n/a
 
      11-23-2003
Jarmo wrote:
> "Nicolas" <> wrote in message
> news:3fbfeb99$0$27046$...
>
>>On most implementations of the standard C library, the functions that
>>copy characters in a previously allocated buffer, like strcpy or strcat,
>>are returning the pointer to that buffer.
>>
>>On my NetBSD system, man strcpy gives the following prototype :
>>char *strcpy(char * restrict dst, const char * restrict src);
>>
>>What's the point in returning a pointer we already know before the call?
>>
>>Thank you in advance for an explanation.

>
>
> So that you can write (unnecessarily abbreviated) code like this:
>
> strcat(path, strcpy(file, "fred.txt"));
>
>


Lol, of course this is obvious.
That's because I never (well almost) enclose a call in another call.
Thx.

 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      11-23-2003
"Ben Pfaff" <> wrote:
> CBFalconer <> writes:
> > It allows you to be baffled by the peculiar run-time actions of:
> >
> > char p[100] = "Fred";
> > ....
> > printf("%s or %s\n", p, strcat(p, " and George"));

>
> That code has undefined behavior, you know.


It does? Sorry, I don't get it.

Assuming you make it into a complete program:

#include <stdio.h>
#include <string.h>

int main(void)
{
char p[100] = "Fred";
printf("%s or %s\n", p, strcat(p, " and George"));
return 0;
}

--
Simon.


 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      11-23-2003
Richard Heathfield wrote:
>
> Nicolas wrote:
>
> >>
> >> strcat(path, strcpy(file, "fred.txt"));

> >
> > Lol, of course this is obvious.
> > That's because I never (well almost) enclose a call in another call.

>
> It's probably wise to never (well almost) enclose a call in another call.
> Many functions return error indicators which can be missed by such an
> approach.
>

But surely many (most?) are expressions with value. I would use the
trick above without batting an eye. I some string manipulation stuff I
wrote..

char *ltrim(char *s); /* remove leading whitespace, return s */
char *rtrim(char *s); /* remove trailing whitespace, return s */

char *alltrim(char *s) {
return ltrim(rtrim(s));
}
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
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
Which are the best manner to return a pointer to a pointer of two-dim array? decker C Programming 9 11-10-2007 12:17 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments