B
Bill Cunningham
Is there a specific reason for using strcpy and memcpy for different
uses? They look to me like the same thing.
Bill
uses? They look to me like the same thing.
Bill
In said:Is there a specific reason for using strcpy and memcpy for different
uses? They look to me like the same thing.
Is there a specific reason for using strcpy and memcpy for different
uses? They look to me like the same thing.
You are ulminately moving values. If I've been understanding right. YouJohn Gordon said:strcpy() takes two arguments. memcpy() takes three arguments. How can
they
look the same?
Is there a specific reason for using strcpy and memcpy for different
uses? They look to me like the same thing.
Bill
memcpy is somewhat more safe. Memcpy doesn't manipulate C strings,
only bytes, it knows nothing of zero termination it copies without
judgement of the content it's copying. It takes three arguments, a
destination, a source and a length. As long as length is <= the size
of destination the copy will only copy the number of bytes that
destination is expected to hold. Of course, you can still create
problems for yourself if the source and destinations overlap or if you
use a length greater than the destination.
WTF!---------------^^^[...]
memcpy is somewhat more safe. Memcpy doesn't manipulate C strings,
only bytes, it knows nothing of zero termination it copies without
judgement of the content it's copying. It takes three arguments, a
destination, a source and a length. As long as length is <= the size
of destination the copy will only copy the number of bytes that
destination is expected to hold. Of course, you can still create
problems for yourself if the source and destinations overlap or if you
use a length greater than the destination.
char *a="String one";
char *b="String two";
memcpy(a,b,sizeof(a+b));
No, it's not even close.Is that a "strcpy" like representation? Atleast to an extent.
Geoff said:They are quite different.
As the name implies, strcpy expects a valid C string in the source
argument. A C string is a string of non-zero characters terminated by
a zero character. If it's not a valid C string it will copy
everything, including data you don't expect, up to the first zero
char, into the destination. This can overflow your destination,
leading to unexpected and unintended behavior in your program.
memcpy is somewhat more safe. Memcpy doesn't manipulate C strings,
only bytes, it knows nothing of zero termination it copies without
judgement of the content it's copying. It takes three arguments, a
destination, a source and a length. As long as length is <= the size
of destination the copy will only copy the number of bytes that
destination is expected to hold. Of course, you can still create
problems for yourself if the source and destinations overlap or if you
use a length greater than the destination.
Bill Cunningham said:Is there a specific reason for using strcpy and memcpy for different
uses? They look to me like the same thing.
WTF!---------------^^^
That is wrong on two levels:
1. You don't SUM two lengths to get the destination length.
2. You don't SUM two pointers to get the length of anything.
No, it's not even close.
If you are going to use strings explicitly then use strcpy.
Bill Cunningham said:You are ulminately moving values. If I've been understanding right. You
can use casts with memcpy and pretty much move strings.
Everything a value
in memory. I have never used these two functions. I was just wondering if
anyone out there has
and what's the practical uses of having two functions
that do the samething it seems to me anyway.
Ben Bacarisse said:No, you are not understanding correctly. No cast is needed to copy a
string using memcpy.
Bill Cunningham said:[...]
WTF!---------------^^^
That is wrong on two levels:
1. You don't SUM two lengths to get the destination length.
2. You don't SUM two pointers to get the length of anything.
No, it's not even close.
If you are going to use strings explicitly then use strcpy.
Sorry about my bad code. I am saying a "C String" *is* a bytestream. void*
can take the char * and You can manipulate that 3rd parameter and use use
memcpy. For example.
strtol (a,NULL,10)
is essentially
atoi(a)
excepting aoti doesn't return error code. So we need strtol. And you can
course convert to all but some of the str in strtol.
strtol (a,b,10);
Am I getting this out? C Strings are /unraw/ C bytes. They can /somehow/
represent strings and be used like strcpy.
K That's I'm done. I can't really and any more.
Keith Thompson said:They're not. RTFM.
Keith Thompson said:They're not. RTFM.
Andrew Smallshaw said:strcpy() copys C strings - i.e. it stops as soon as it encounters
a terminating NULL character. memcpy() copies the specified number
of bytes regardless of the contents of those bytes.
[...]
memcpy is somewhat more safe. Memcpy doesn't manipulate C strings,
only bytes, it knows nothing of zero termination it copies without
judgement of the content it's copying. It takes three arguments, a
destination, a source and a length. As long as length is <= the size
of destination the copy will only copy the number of bytes that
destination is expected to hold. Of course, you can still create
problems for yourself if the source and destinations overlap or if you
use a length greater than the destination.
char *a="String one";
char *b="String two";
memcpy(a,b,sizeof(a+b));
Is that a "strcpy" like representation? Atleast to an extent.
WTF!---------------^^^
Bill Cunningham said:"Geoff" <[email protected]> wrote in message
[...]Sorry about my bad code. I am saying a "C String" *is* a bytestream. void*
can take the char * and You can manipulate that 3rd parameter and use use
memcpy. For example.
+1 !!!!!!!Material this good can only come from having great staff.
Kudos to the talented writers behind "Late Night with Bill Cunningham"!
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.