欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標題: strncpy函數實現及簡單應用 [打印本頁]

作者: xiaojuan    時間: 2014-9-24 22:30
標題: strncpy函數實現及簡單應用
//:Vc++6.0  String  strncpy函數
//功能:指定大小的字符串拷貝
//參數:dest 目標字符串  str 要考的字符串  num 拷貝大小
//返回值:dest字符串地址
#include<stdio.h>

char *strncpy(char *dest, const char *str, int num);

int main()
{
char str1[]= "To be or not to be";
char str2[6];
strncpy (str2,str1,5);
printf("str2 = %s\n", str2);

return 0;
}

char *strncpy(char * dest, const char *str, int num)
{
//查錯
    if (dest == NULL || str == NULL)
    {
perror("dest or str");
return NULL;
    }
//求長度
char *temp1 = dest;
const char *temp2 = str;
    int len;
while (*(temp2++) != '\0');
len = temp2 - str;

//拷貝
    if (num <= 0)
        *temp1 = '\0';
    else  if (num >= len)
    {
        while (*str != '\0')
            *(temp1++) = *(str++);
        *temp1 = '\0';
    }
    else
    {
int i;
        for (i = 0; i < num; i++)
        {
            *(temp1++) = *(str++);
        }
        *temp1 = '\0';
    }
return dest;
}
//在vc++6.0中的運行結果為:str2 = To be
//注:在拷貝時已經在尾部加了'\0'//:~






歡迎光臨 (http://www.raoushi.com/bbs/) Powered by Discuz! X3.1