代码审查:strncpy [英] Code Review: strncpy
问题描述
根据C99,以下是strncpy实施
?
char *
strncpy(char * s,const char * t,size_t n)
{
char * p = s;
size_t i = 0;
if(!s)
返回s;
while(t& ;& i< n)
{
* s ++ = * t ++;
i ++;
}
if(!t)
{
do
* s ++ =''\ 0'';
while(i ++< n);
}
返回p;
}
谢谢
-
Vijay Kumar R Zanvar
Hi,
Is the following strncpy implementation
according to C99?
char *
strncpy ( char *s, const char *t, size_t n )
{
char *p = s;
size_t i = 0;
if ( !s )
return s;
while ( t && i < n )
{
*s++ = *t++;
i++;
}
if ( !t )
{
do
*s++ = ''\0'';
while ( i++ < n );
}
return p;
}
Thanks
--
Vijay Kumar R Zanvar
推荐答案
Vijay Kumar R Zanvar写道:
Vijay Kumar R Zanvar wrote:
根据C99,以下是strncpy实施吗?
char *
strncpy(char * s,const char * t,size_t n)
{
char * p = s;
size_t i = 0;
如果( !s)
返回s;
while(t&&我< n)
ITYM:
while(* t&& i< n)
{
* s ++ = * t ++;
i ++;
}
如果(!t)
ITYM:
if(* t == 0)
{
*
* s ++ =''\ 0'';
while(i ++< n) ;
哎哟!!如果* t == 0且i == n会发生什么?
-nrk。
}
返回p;
}
谢谢
-
Vijay Kumar R Zanvar
Hi,
Is the following strncpy implementation
according to C99?
char *
strncpy ( char *s, const char *t, size_t n )
{
char *p = s;
size_t i = 0;
if ( !s )
return s;
while ( t && i < n )
ITYM:
while ( *t && i < n )
{
*s++ = *t++;
i++;
}
if ( !t )
ITYM:
if ( *t == 0 )
{
do
*s++ = ''\0'';
while ( i++ < n );
Ouch!! What happens if *t == 0 and i == n?
-nrk.
}
return p;
}
Thanks
--
Vijay Kumar R Zanvar
Vijay Kumar R Zanvar < 6 ***** @ hotpop.com>在留言中写道
news:bu ************ @ ID-203837.news.uni-berlin.de ...
"Vijay Kumar R Zanvar" <vi*****@hotpop.com> wrote in message
news:bu************@ID-203837.news.uni-berlin.de...
你好,
根据C99,以下是strncpy实现吗?
char *
strncpy(char * s,const char * t,size_t n)
strncpy(char * restrict s,const char * restrict t,size_t n)
{
char * p = s;
你永远不会改变p。我会声明它为char * const p;
size_t i = 0;
if(!s)
return s;
标准并未强制执行此项检查。但是如果你已经这样做了,为什么不检查?t
呢?
而(t& i< n< n)
while(* t&& i< n)
{
* s ++ = * t ++;
i ++;
}
if(!t)
if(!* t)
{
do
* s ++ =''\ 0'';
while(i ++< n);
}
返回p;
}
Hi,
Is the following strncpy implementation
according to C99?
char *
strncpy ( char *s, const char *t, size_t n )
strncpy ( char * restrict s, const char * restrict t, size_t n )
{
char *p = s;
You never change p. I would declare it as char * const p;
size_t i = 0;
if ( !s )
return s;
Standard does not mandate this check. But if you do it already, why not
checking for !t as well?
while ( t && i < n )
while ( *t && i < n )
{
*s++ = *t++;
i++;
}
if ( !t )
if ( !*t )
{
do
*s++ = ''\0'';
while ( i++ < n );
}
return p;
}
除了缺少星号和限制关键字,我认为应该没问题。
我也会替换i ++< n在循环中使用n--并且除去额外的
变量,但那是'我,不是你;-)
Peter
Apart from missing asterisks and restrict keywords, I think it should be OK.
I would also replace i++ < n in the loops with n-- and get rid of an extra
variable, but that''s me, not you ;-)
Peter
非常感谢nrk和Peter ....
小错误但影响很大!
/ *修改* /
char *
strncpy(char * restrict s,const char * restrict t,size_t n)
{
char * const p = s;
if(!s ||!t)
返回s;
while(* t&& n)
{
* s ++ = * t ++;
n--;
}
if(!* t&& n)
{
do
* s ++ =''\ 0'';
while(n - );
}
返回p;
}
Thanks a lot nrk and Peter....
Small mistakes but big affects!
/* modified */
char *
strncpy ( char * restrict s, const char * restrict t, size_t n )
{
char * const p = s;
if ( !s || !t )
return s;
while ( *t && n )
{
*s++ = *t++;
n--;
}
if ( !*t && n )
{
do
*s++ = ''\0'';
while ( n-- );
}
return p;
}
这篇关于代码审查:strncpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!