一个关于strncpy的问题

QlDoors 2009-07-10 08:14:59
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int find_num_string(char *outputstr, char *inputstr)
{
char *in = inputstr, *out = outputstr, *temp, *final;
int count = 0, maxlen = 0, i;

while(*in != '\0')
{
if((*in > 47) && (*in < 58))
{
for(temp = in; (*in > 47) && (*in < 58); in++)
count++;
}
else
in++;

if(maxlen < count)
{
maxlen = count;
final = temp;
}

count = 0;
}

strncpy(out , final, maxlen);

return maxlen;
}

int main()
{
char *s = "sdfs12321dfd13214327483asd2332";
char *p = (char *)malloc(strlen(s) + 1);
int count = find_num_string(p, s);
printf("%s\nlength = %d\n",p,count);

return 0;
}

函数find_num_strting实现功能:找出字符串中最长的联系数字串,并返回长度。
这里用到了strncpy,关于strncpy的用法书上是这么说的:如果源串长度大于n,则strncpy不复制最后的'\0'结束符,所以是不安全的,复制完后需要手动添加字符串的结束符才行。
这里我并没有手动添加'\0',程序运行结果正常,这个难道书上说错么?请教高手解释下,谢谢。
...全文
600 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
QlDoors 2009-07-11
  • 打赏
  • 举报
回复
是不是字符串初始化为null了?
baihacker 2009-07-11
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 QlDoors 的回复:]
引用 12 楼 shen8686 的回复:
strncpy:将字符串strSource中前count个字符拷贝到字符串destination中。
如果strSource长度没count,会自动补null给strDest到count
如果strSource长度比count则,只copy count个字符,不会出错的啊
不然要strncpy有什么用?
strncpy的作用本来就是只copy strSource的前count个字符嘛


问题是copy count个字符后加不加null
[/Quote]
...因为是这样设计的...
QlDoors 2009-07-11
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 shen8686 的回复:]
strncpy:将字符串strSource中前count个字符拷贝到字符串destination中。
如果strSource长度没count,会自动补null给strDest到count
如果strSource长度比count则,只copy count个字符,不会出错的啊
不然要strncpy有什么用?
strncpy的作用本来就是只copy strSource的前count个字符嘛
[/Quote]
问题是copy count个字符后加不加null
drinker_linux 2009-07-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 baihacker 的回复:]
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 …
[/Quote]
顶这个
shen8686 2009-07-11
  • 打赏
  • 举报
回复
不过这样有个前提:
destination是初始化过的
若初始化过,strncpy复制的字符串会添加到null前
否则会出错
shen8686 2009-07-11
  • 打赏
  • 举报
回复
strncpy:将字符串strSource中前count个字符拷贝到字符串destination中。
如果strSource长度没count,会自动补null给strDest到count
如果strSource长度比count则,只copy count个字符,不会出错的啊
不然要strncpy有什么用?
strncpy的作用本来就是只copy strSource的前count个字符嘛
QlDoors 2009-07-11
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 baihacker 的回复:]

如果源串长度大于n
我看反了...

在楼主的情况下
final的长度是不可能大于maxlen的啊...
[/Quote]
题中的情况final应该大于maxlen的
一般情况下,final >= maxlen
final只是给出起始地址

另外我写了个小程序试了下,count小于src长度时,后面不手动加入NULL也正常输出字符串
实在不解啊
dongchaotiantian 2009-07-10
  • 打赏
  • 举报
回复
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.

如果count 小于源字符串长度,不会自动加上NULL,
如果count 大于源字符串长度,会自动加上NULL。
final的长度是从数字开始,直到字符串的末尾。
而maxlen的大小是数字的计数。
所以final的长度大于maxlen,属于第一种情况。
因此显示的字符串会是数字和一些乱码。
baihacker 2009-07-10
  • 打赏
  • 举报
回复

如果源串长度大于n
我看反了...

在楼主的情况下
final的长度是不可能大于maxlen的啊...
jenglev 2009-07-10
  • 打赏
  • 举报
回复
学习了
QlDoors 2009-07-10
  • 打赏
  • 举报
回复
char *strncpy(char *dest,const char *src,size_t count)
{
char *tmp = dest;

while(count-- && (*dest++ = *src++) != '\0')
/*nothing*/;

retrun tmp;
}
QlDoors 2009-07-10
  • 打赏
  • 举报
回复
char * my_strncpy( char * dest, const char * source, int count )

{

char *p = dest;

while (count && (*p++ = *source++)) count--;

while(count--)

*p++ = '\0';

return(dest);

}

baihacker 2009-07-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 QlDoors 的回复:]
引用 1 楼 baihacker 的回复:
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…
[/Quote]
和题目中情况有啥区别?
你只是说strncpy的要求...所以不用管其它的...
关于strncpy的用法书上是这么说的:如果源串长度大于n,则strncpy不复制最后的'\0'结束符,所以是不安全的,复制完后需要手动添加字符串的结束符才行。
meipen 2009-07-10
  • 打赏
  • 举报
回复
#include <string.h>
char *strncpy( char *to, const char *from, size_t count );


功能:将字符串from 中至多count个字符复制到字符串to中。如果字符串from 的长度小于count,其余部分用'\0'填补。返回处理完成的字符串。

QlDoors 2009-07-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 baihacker 的回复:]
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 …
[/Quote]
这个情况跟程序中情况不一样吧
pengzhixi 2009-07-10
  • 打赏
  • 举报
回复
去看看strncpy的源码就知道了,我记得好像这个函数是会在结尾加上'\0'这个结束符的
baihacker 2009-07-10
  • 打赏
  • 举报
回复
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.

69,382

社区成员

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

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