strncpy源码

//

//  main.cpp

//  AUTO_PRO

//

//  Created by yanzhengqing on 12-12-11.

//  Copyright (c) 2012 yanzhengqing. All rights reserved.

//


#include <iostream>

using namespace std;


/***

 *char *strncpy(dest, source, count) - copy at most n characters

 *

 *Purpose:

 *       Copies count characters from the source string to the

 *       destination.  If count is less than the length of source,

 *       NO NULL CHARACTER is put onto the end of the copied string.

 *       If count is greater than the length of sources, dest is padded

 *       with null characters to length count.

 *

 *

 *Entry:

 *       char *dest - pointer to destination

 *       char *source - source string for copy

 *       unsigned count - max number of characters to copy

 *

 *Exit:

 *       returns dest

 *

 *Exceptions:

 *

 *******************************************************************************/



/

/*说明:

  1. __cdecl C Declaration的缩写(declaration,声明),表示C语言默认的函数调用方法:所有参数从右到左依次入栈,这些参数由调用者清除,称为手动清栈。被调用函数不会要求调用者传递多少参数,调用者传递过多或者过少的参数,甚至完全不同的参数都不会产生编译阶段的错误。

  2. 将一个字符串从一个位置复制到另一个位置,最多复制n个字节

  3.  按照ANSI(American National Standards Institute)标准,不能对void指针进行算法操作,即不能对void指针进行如p++的操作,所以需要转换为具体的类型指针来操作,例如char *。(引用网友的结论)

  4.  size_t 类型定义在cstddef头文件中,该文件是C标准库的头文件stddef.hC++版。它是一个与机器相关的unsigned类型,其大小足以保证存储内存中对象的大小。

*/


char * __cdecl strncpy (

                       char * dest,

                       constchar * source,

                       size_t count

                        )

{

   char *start = dest;

    

   while (count && (*dest++ = *source++))   /* copy string */

        count--;

    

   if (count)                             /* pad out with zeroes */

       while (--count)

            *dest++ ='\0';

    

   return(start);

}



int main()

{

   char src[50] ="";

    constchar brc[50] ="blog.csdn.net/barry_yan";

   cout<<src<<endl;

   cout<<brc<<endl;

   strncpy(src,brc,strlen(brc));

   cout<<src<<endl;

   return0;

}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值