前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c语言之利用指针复制字符串的几种形式

c语言之利用指针复制字符串的几种形式

作者头像
西西嘛呦
发布2020-08-26 21:21:18
2.4K0
发布2020-08-26 21:21:18
举报

第一种:

代码语言:javascript
复制
#include<stdio.h>
#include<iostream>

void copy_string(char* p1, char* p2) {
    for (; *p1 != '\0'; *p1++,*p2++)
    {
        *p2 = *p1;
    }
    *p2 = '\0';
}

int main() {
    char* str1 = (char*) "hello world";
    char str2[] = "i am a student";
    copy_string(str1, str2);
    printf("%s\n",str2);
    system("pause");
    return 0;
}

第二种:

代码语言:javascript
复制
#include<stdio.h>
#include<iostream>

void copy_string(char* p1, char* p2) {
    while ((*p2 = *p1) != '\0')
    {
        *p2++;
        *p1++;
    }
}

int main() {
    char* str1 = (char*)"hello world";
    char str2[] = "i am a student";
    copy_string(str1, str2);
    printf("%s\n", str2);
    system("pause");
    return 0;
}

第三种:

代码语言:javascript
复制
#include<stdio.h>
#include<iostream>

void copy_string(char* p1, char* p2) {
    //指针运算符比++优先级高
    //也就是先将*p1的值给*p2,再进行++操作,i++是先赋值,后自增
    while ((*p2++ = *p1++) != '\0')
}

int main() {
    char* str1 = (char*)"hello world";
    char str2[] = "i am a student";
    copy_string(str1, str2);
    printf("%s\n", str2);
    system("pause");
    return 0;
}

第四种:

代码语言:javascript
复制
#include<stdio.h>
#include<iostream>

void copy_string(char* p1, char* p2) {
    while (*p1 != '\0') {
        *p2++ = *p1++;
    }
    *p2 = '\0';
}

int main() {
    char* str1 = (char*)"hello world";
    char str2[] = "i am a student";
    copy_string(str1, str2);
    printf("%s\n", str2);
    system("pause");
    return 0;
}

第五种:

代码语言:javascript
复制
#include<stdio.h>
#include<iostream>

void copy_string(char* p1, char* p2) {
    //当*p2++ = *p1++变为0时,就会结束循环
    while (*p2++ = *p1++) {
        ; //'\0' == 0;结束标志
    }
}

int main() {
    char* str1 = (char*)"hello world";
    char str2[] = "i am a student";
    copy_string(str1, str2);
    printf("%s\n", str2);
    system("pause");
    return 0;
}

第六种:

代码语言:javascript
复制
#include<stdio.h>
#include<iostream>

void copy_string(char* p1, char* p2) {
    for (; *p2++ = *p1++;) {
        ; //'\0' == 0;结束标志
    }
}

int main() {
    char* str1 = (char*)"hello world";
    char str2[] = "i am a student";
    copy_string(str1, str2);
    printf("%s\n", str2);
    system("pause");
    return 0;
}

第七种:

代码语言:javascript
复制
#include<stdio.h>
#include<iostream>

void copy_string(char str1[], char str2[]) {
    char* p1, * p2;
    p1 = str1;
    p2 = str2;
    while((*p2++ = *p1++)!='\0') {
        ; //'\0' == 0;结束标志
    }
}

int main() {
    char* str1 = (char*)"hello world";
    char str2[] = "i am a student";
    copy_string(str1, str2);
    printf("%s\n", str2);
    system("pause");
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-12-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档