P
pauldepstein
This code comes from cpluplus.com
/* memcpy example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
memcpy (str2,str1,strlen(str1)+1);
memcpy (str3,"copy successful",16);
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
return 0;
}
I'm sure it's ok but I'm concerned about the fact that memcpy takes
void* (and const void*) parameters.
Why is it unnecessary to cast str1, str2 and str3 to type char* before
the final printf statement?
For example, I read this on another website: "One of the problems with
using void * is that as soon as you convert something to a void * all
information about what type of thing the pointer points at is lost.
There is absolutely _no_ guarantee that the pointer type the void * is
cast back to matches that from which it was created."
But, to me, the above code seems to assume that a void pointer points
to char.
Thank you,
Paul Epstein
/* memcpy example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
memcpy (str2,str1,strlen(str1)+1);
memcpy (str3,"copy successful",16);
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
return 0;
}
I'm sure it's ok but I'm concerned about the fact that memcpy takes
void* (and const void*) parameters.
Why is it unnecessary to cast str1, str2 and str3 to type char* before
the final printf statement?
For example, I read this on another website: "One of the problems with
using void * is that as soon as you convert something to a void * all
information about what type of thing the pointer points at is lost.
There is absolutely _no_ guarantee that the pointer type the void * is
cast back to matches that from which it was created."
But, to me, the above code seems to assume that a void pointer points
to char.
Thank you,
Paul Epstein