赋值函数

#include <stdio.h>
#include <stdlib.h>
// 数组赋值三种方式

void mystrcpy(char *str1, const char *str2)
{
    // *str2对*str1逐个字符进行赋值
    // *str2直到把''赋值给*str1时,*str1的结果就是0,循环就结束!
    while ((*str1++ == *str2++));
    
}

int main()
{
    char str[10] = "abc";

    // 使用循环给字符数组赋值
    for (int i = 0; i < 10; i++)
    {
        str[i] = "ABC"[i];  // 等价于 *("ABC"+i),"ABC"返回的是A的地址(即首地址)==> 替换
        printf("%s
", str);  //str = ABC
    }

    // 使用标准库函数给字符数组赋值
    strcpy(str, "XYZ");
    printf("str = %s
", str);

    // 使用自定义函数给字符数组赋值
    mystrcpy(str, "OKOK");
    printf("str = %s
", str);


    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/nothx/p/8488709.html