C 字符串拷贝

来源:互联网 发布:holy cross学院知乎 编辑:程序博客网 时间:2024/06/05 00:26



#include<stdio.h>#include<string.h>int str_cpy(char *destination, const char*source){if (destination == NULL || source == NULL){return -1;}for (; *source != '\0'; ){*destination++ = *source++;}*destination = '\0';return 0;}int str_cpy2(char *destination, const char*source){if (destination == NULL || source == NULL){return -1;}while ((*destination++ = *source++) != '\0');return 0;}int main(){char p[16];char *q = "hello world";strcpy(p, q);memset(p, 0x00, sizeof(p));str_cpy(p, q);memset(p, 0x00, sizeof(p));str_cpy2(p, q);memset(p, 0x00, sizeof(p));return 0;}


0 0
原创粉丝点击