/* Public domain. */
#include<stddef.h>
void*
memcpy (void*dest,constvoid*src, size_t len)
{
char*d= dest;
constchar*s= src;
while(len--)
*d++=*s++;
return dest;
}
//参数没有类型之分 是以字节为单位,放回第一个参数的指针
#include <stdio.h>
#include <string.h>
int main ()
{
const char src[50] = "http://www.tutorialspoint.com";
char dest[50];
printf("Before memcpy dest = %s\n", dest);
memcpy(dest, src, strlen(src)+1);
printf("After memcpy dest = %s\n", dest);
return(0);
}
Before memcpy dest =
After memcpy dest = http://www.tutorialspoint.com
char* strcpy(char*to,constchar*from);
char* strcpy(char*to,constchar*from)
{
char*save= to;
for(;(*to=*from);++from,++to);
return save;
}
//以/0为结束
#include <stdio.h>
#include <string.h>
int main()
{
char src[40];
char dest[100];
memset(dest, '\0', sizeof(dest));
strcpy(src, "This is tutorialspoint.com");
strcpy(dest, src);
printf("Final copied string : %s\n", dest);
return(0);
}
Let us compile and run the above program, this will produce the following result:
Final copied string : This is tutorialspoint.com
#include "LibC.h"
char*
strncpy(char* dest, const char*
src, size_t n)
{
char * ptr =
dest;
while (n && (*ptr = *src))
{
++ptr;
++src;
--n;
}
while (n--)
*ptr++ = '\0';
return dest;
}
//尽量让n大这样可以防止dest中没有/0
#include <stdio.h>
#include <string.h>
int main()
{
char src[40];
char dest[12];
memset(dest, '\0', sizeof(dest));
strcpy(src, "This is tutorialspoint.com");
strncpy(dest, src, 10);
printf("Final copied string : %s\n", dest);
return(0);
}
Final copied string : This is tu
#include<stddef.h>
void*
memcpy (void*dest,constvoid*src, size_t len)
{
char*d= dest;
constchar*s= src;
while(len--)
*d++=*s++;
return dest;
}
//参数没有类型之分 是以字节为单位,放回第一个参数的指针
#include <stdio.h>
#include <string.h>
int main ()
{
const char src[50] = "http://www.tutorialspoint.com";
char dest[50];
printf("Before memcpy dest = %s\n", dest);
memcpy(dest, src, strlen(src)+1);
printf("After memcpy dest = %s\n", dest);
return(0);
}
Before memcpy dest =
After memcpy dest = http://www.tutorialspoint.com
char* strcpy(char*to,constchar*from);
char* strcpy(char*to,constchar*from)
{
char*save= to;
for(;(*to=*from);++from,++to);
return save;
}
//以/0为结束
#include <stdio.h>
#include <string.h>
int main()
{
char src[40];
char dest[100];
memset(dest, '\0', sizeof(dest));
strcpy(src, "This is tutorialspoint.com");
strcpy(dest, src);
printf("Final copied string : %s\n", dest);
return(0);
}
Let us compile and run the above program, this will produce the following result:
Final copied string : This is tutorialspoint.com
#include "LibC.h"
char*
strncpy(char* dest, const char*
src, size_t n)
{
char * ptr =
dest;
while (n && (*ptr = *src))
{
++ptr;
++src;
--n;
}
while (n--)
*ptr++ = '\0';
return dest;
}
//尽量让n大这样可以防止dest中没有/0
#include <stdio.h>
#include <string.h>
int main()
{
char src[40];
char dest[12];
memset(dest, '\0', sizeof(dest));
strcpy(src, "This is tutorialspoint.com");
strncpy(dest, src, 10);
printf("Final copied string : %s\n", dest);
return(0);
}
Final copied string : This is tu