473,761 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strcpy and strcat's return type

Hi,

Why do strcpy and strcat (and strupr and strlwr in some nonstandard
implementations ) return a char*? Surely the logical (and DMA-safe)
)return type for these would have been void??

Thanks,

James McLaughlin.
Nov 15 '05 #1
14 3716
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ze************* *@yahoo.com wrote:
Hi,

Why do strcpy and strcat (and strupr and strlwr in some nonstandard
implementations ) return a char*? Surely the logical (and DMA-safe)
)return type for these would have been void??


Why should strcat() return a void *, when both of it's arguments are
char *, and the function is defined as working on character strings?

The same goes for strcpy(): why do you think that it should return void
* rather than char *?
ISTM that, since both functions
a) take char * as arguments (and not void *),
b) work on char * data, and
c) create results best described by char * pointers,
both functions /should/ return char * pointers to the results that they
have generated.

- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFC5ickagV FX4UWr64RAmWKAJ 9ymd+jL7wIqVGgr HNTo198BnxJDgCg rXyr
BGAvh0N9wubb8W9 NJLmfxIA=
=6TrQ
-----END PGP SIGNATURE-----
Nov 15 '05 #2
Lew Pitcher:
ze************* *@yahoo.com:

....
Why do strcpy and strcat (and strupr and strlwr in some nonstandard
implementations ) return a char*? Surely the logical (and DMA-safe)
)return type for these would have been void??


Why should strcat() return a void *, when both of it's arguments are
char *, and the function is defined as working on character strings?


He said void, not void *. And to answer the actual question: I think,
they return char * to allow such things as

return strcat(strcpy(b uf, path), file);

Jirka
Nov 15 '05 #3
Jirka Klaue wrote:

Lew Pitcher:
ze************* *@yahoo.com:

...
Why do strcpy and strcat (and strupr and strlwr in some nonstandard
implementations ) return a char*? Surely the logical (and DMA-safe)
)return type for these would have been void??


Why should strcat() return a void *, when both of it's arguments are
char *, and the function is defined as working on character strings?


He said void, not void *. And to answer the actual question: I think,
they return char * to allow such things as

return strcat(strcpy(b uf, path), file);


Besides, wasn't there originally no such thing as a void function? (I
know I worked on platforms without "void".)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Nov 15 '05 #4

<ze************ **@yahoo.com> wrote

Why do strcpy and strcat (and strupr and strlwr in some nonstandard
implementations ) return a char*? Surely the logical (and DMA-safe)
)return type for these would have been void??

History. Originally there was no such keyword as "void" so a function
returned a garbage integer instead of nothing.
They would be better off being void functions now, but that would break back
compatibility.
Nov 15 '05 #5
Me
ze************* *@yahoo.com wrote:
Why do strcpy and strcat (and strupr and strlwr in some nonstandard
implementations ) return a char*?
So you can compose functions without storing it in intermediate
variables.
Surely the logical (and DMA-safe) return type for these would have
been void??


If your compiler generates code that doesn't treat these basic
string/block of memory functions as if the return type was void when
the return value is not used (on a sufficient optimization level), get
a better compiler or write your own versions of these functions.

Nov 15 '05 #6
"Me" <an************ *****@yahoo.com > writes:
ze************* *@yahoo.com wrote:
Why do strcpy and strcat (and strupr and strlwr in some nonstandard
implementations ) return a char*?
So you can compose functions without storing it in intermediate
variables.


And for backwards compatibility; as others have mentioned here, these
functions predate the existence of void.
Surely the logical (and DMA-safe) return type for these would have
been void??


I have no idea what "zeroDontSpamty pe" meant by "DMA-safe". Perhaps
it's some off-topic implementation detail. (The most familiar
expansion of DMA is Direct Memory Access, but I don't see how that's
relevant here.)
If your compiler generates code that doesn't treat these basic
string/block of memory functions as if the return type was void when
the return value is not used (on a sufficient optimization level), get
a better compiler or write your own versions of these functions.


That's probably a useful optimization, but I don't see that it's a
critical one. The overhead of returning and discarding the result is
likely to be trivial. (Though if the call is inlined, it's likely
that the code to generate the result will be discarded anyway.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #7
Keith Thompson wrote:
[...]
Surely the logical (and DMA-safe) return type for these would have
been void??


I have no idea what "zeroDontSpamty pe" meant by "DMA-safe". Perhaps
it's some off-topic implementation detail. (The most familiar
expansion of DMA is Direct Memory Access, but I don't see how that's
relevant here.)


Perhaps strcpy() is on the do-not-call list?

[...]

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Nov 15 '05 #8
Hi,

By DMA I meant "Dynamic Memory Allocation" although as strupr isn't in
the standard it's sort of do-not-call:-)

The fact that pointers are passed to these functions, so the result
returned has in a way already been returned - hence void being
logical.

I realise now that, unless the implementation is really bad, (ie if
malloc or calloc is used in the function to create a new variable for
the return data - often this is necessary, but not for these functions
which can use one of the input strings), DMA safety won't be an issue
- it's just I've had problems with * returning functions using
malloc/calloc/new (in C++) to create something to return which
couldn't subsequently be deleted in certain circumstances.

Thanks all,

James M.
Nov 15 '05 #9
ze************* *******@yahoo.c om wrote:
Hi,

By DMA I meant "Dynamic Memory Allocation" although as strupr isn't in
the standard it's sort of do-not-call:-)

The fact that pointers are passed to these functions, so the result
returned has in a way already been returned - hence void being
logical.

I realise now that, unless the implementation is really bad, (ie if
malloc or calloc is used in the function to create a new variable for
the return data - often this is necessary, but not for these functions
which can use one of the input strings), DMA safety won't be an issue
- it's just I've had problems with * returning functions using
malloc/calloc/new (in C++) to create something to return which
couldn't subsequently be deleted in certain circumstances.

Thanks all,

James M.


I thought DMA stood for "Direct Memory Access", as in - DMA chips... :o?
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
11616
by: JC | last post by:
hi, i want to combine two string together.. and put in to another string. how can i do . i try myself.. with the follow code. but seem can't get the result i want.. i want to get the result with "c is abcd" . #include <stdio.h> #include <string.h> void main() { char a="ab"; char b="cd";
44
5812
by: Nicolas | last post by:
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.
9
12791
by: Pascal Damian | last post by:
I read somewhere that strcpy() is safer when dealing with malloc()-ed strings. Is that true? (Of course I know that both are unsafe). -- Pascal
81
7342
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
24
3147
by: diego.arias.vel | last post by:
Hi all I have certain problem when I'm doing this: void copy(char *filename) { char *log_file; log_file=filename; strcat(log_file,"-log.txt");
3
16824
by: kaizen | last post by:
Hi, i wrote the code in C and compiled in VC++ compiler. at that time it has thrown the below mentioned error. error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
55
7098
by: Jake Thompson | last post by:
I need to copy a value into a char * field. I am currently doing this strcpy(cm8link.type,"13"); but I get an error of error C2664: 'strcpy' : cannot convert parameter 1 from 'const char' to 'char *'
13
1936
by: steve yee | last post by:
strcpy() returns it's input parameter. i asked a similar question on c++, most people seem to not think it as a problemic design. how do you think of this? http://groups.google.com/group/comp.lang.c++/browse_thread/thread/fd0aa4307b91bad8
4
9537
by: chikito.chikito | last post by:
1. Can someone tell me the difference between these two functions: void strcpy(char *s1, const char *s2) { while(*s1++ = *s2++) ; } //function prototype of strcpy follows char *strcpy(char *s1, const char *s2) // library function
0
9377
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.