谁知道 strncpy 这个函数是做什么用的?他的语法格式是什么?

loadinghnu 2003-08-18 04:23:05
#include<iostream>
#include<string.h>
using namespace std;
class student
{
public:
student(char * pname)
{
cout<<"constructing student"<<pname<<endl;
strncpy(name,pname,sizeof(name));
name[sizeof(name)-1]='\0';
}
~student()
{
cout<<"destructing"<<name<<endl;
}
protected:
char name[20];

};
void main()
{
student ss("jenny");
}


没写错 不是 STRCPY!!!
...全文
73 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
thuers 2003-08-18
  • 打赏
  • 举报
回复
MSDN的标准定义:
char *strncpy(char* strDest,const char* strSour,size_t count);
这个函数的意思是:
拷贝一个字符串到另外一个字符串,strDest 是目标字符串地址,strSour是源字符串地址,
size_t是要拷贝的字符个数,当要拷贝的字符个数(size_t count)小于或等于strSour长度,
strDest的最后不会加\0,当要拷贝的字符个数(size_t count)大于strSour长度,strDest最后会自动追加一个\0,

参见源码:

/***
*strncpy.c - copy at most n characters of string
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines strncpy() - copy at most n characters of string
*
*******************************************************************************/

#include <cruntime.h>
#include <string.h>

/***
*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:
*
*******************************************************************************/

char * __cdecl strncpy (
char * dest,
const char * 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);
}

lybapple 2003-08-18
  • 打赏
  • 举报
回复
char *strncpy( char *strDest, const char *strSource, size_t count );


The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string. If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap.


Example
/* STRNCPY.C */

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

void main( void )
{
char string[100] = "Cats are nice usually";
printf ( "Before: %s\n", string );
strncpy( string, "Dogs", 4 );
strncpy( string + 9, "mean", 4 );
printf ( "After: %s\n", string );
}

Output
Before: Cats are nice usually
After: Dogs are mean usually




loadinghnu 2003-08-18
  • 打赏
  • 举报
回复
strncpy(name,pname,sizeof(name));
name[sizeof(name)-1]='\0';


这两条语句是什么作用?

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧