天天看点

C中 strcpy/strncpy的使用

#include <stdio.h>
#include <string.h>

int main(int argc, const char *argv[])
{
    char a[10]  = "abc";
    char b[100] = "1234567890abcdefghijklmnopqrstuvwxyz";

    //把b的内容拷贝到a里面
    /*
    int index = 0;
    while(b[index])
    {
        a[index] = b[index];
        index++;
    }
    */

//    strcpy(a, b);
    strncpy(a, b, sizeof(a) - 1);
    printf("%s\n", a);
    
    return 0;

}


           

测试结果

C中 strcpy/strncpy的使用

继续阅读