473,387 Members | 1,520 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,387 software developers and data experts.

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 3688
-----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)

iD8DBQFC5ickagVFX4UWr64RAmWKAJ9ymd+jL7wIqVGgrHNTo1 98BnxJDgCgrXyr
BGAvh0N9wubb8W9NJLmfxIA=
=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(buf, 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(buf, 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 "zeroDontSpamtype" 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_Keith) 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 "zeroDontSpamtype" 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.com 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
In article <de**********@volcano1.grnet.gr>,
mike <po*************@yahoo.com> wrote:
ze********************@yahoo.com wrote:
Hi,

By DMA I meant "Dynamic Memory Allocation" although as strupr isn't in
the standard it's sort of do-not-call:-)
....I thought DMA stood for "Direct Memory Access", as in - DMA chips... :o?


Hmm... I always thought DMA stood for "David Michael Anderson". He's my
cousin.

Nov 15 '05 #11
ze********************@yahoo.com 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.


DMA is a standard computer hardware term Direct Memory Access. You don't
know what it means. I do. When you use DMA in some other way, it makes
my head hurt.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #12
Joe Wright wrote
(in article <-_********************@comcast.com>):
ze********************@yahoo.com 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.


DMA is a standard computer hardware term Direct Memory Access. You don't
know what it means. I do. When you use DMA in some other way, it makes
my head hurt.


The problem is that people (even technical people in the
community that should know better) keep using existing acronyms
(and longer terms) over and over again for different meanings.
Perhaps they think that if it was invented while they were in
diapers then it is ripe for replacement.

Somebody once said "We have used up all the good TLAs and now
the FLAs are in jeopardy."
--
Randy Howard (2reply remove FOOBAR)

Nov 15 '05 #13
mike <po*************@yahoo.com> writes:
ze********************@yahoo.com wrote:
Hi,
By DMA I meant "Dynamic Memory Allocation"
[...] I thought DMA stood for "Direct Memory Access", as in - DMA chips... :o?


Mike, you're responding to month-old articles. We already went over
this.

--
Keith Thompson (The_Other_Keith) 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 #14
Randy Howard wrote:
[...]
DMA is a standard computer hardware term Direct Memory Access. You don't
know what it means. I do. When you use DMA in some other way, it makes
my head hurt.


The problem is that people (even technical people in the
community that should know better) keep using existing acronyms
(and longer terms) over and over again for different meanings.
Perhaps they think that if it was invented while they were in
diapers then it is ripe for replacement.

Somebody once said "We have used up all the good TLAs and now
the FLAs are in jeopardy."


s/FLA/ETLA/

:-)

--
+-------------------------+--------------------+-----------------------------+
| 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 #15

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

Similar topics

23
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...
44
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...
9
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
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...
24
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
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 *'...
55
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'...
13
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? ...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.