sizeof(str)或sizeof(str) - 1? [英] sizeof(str) or sizeof(str) - 1 ?

查看:179
本文介绍了sizeof(str)或sizeof(str) - 1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个应该以NULL结尾的字符串,那么使用sizeof(str)这是很好的做法吗?或sizeof(str) - 1或sizeof(str) - 1。什么时候使用像strncpy这样的函数?

如果我有一个''字符串''不应该被NULL终止,那么这是不错的做法

使用" sizeof(str ) - 1或sizeof(str)或sizeof(str)。当使用像

strncpy这样的函数时?


这个函数是否没有错误?


void setname(char *姓名)

{

char null_buf [256];

char buf [256];

strncpy( buf,name,sizeof(buf)); / *不要保留NULL终止符* /

strncpy(null_buf,name,sizeof(buf) - 1); / *保留NULL终结符* /

}

If I have a string that should be NULL terminated, is it good practice to
use "sizeof(str)" or "sizeof(str) - 1" when using a function like strncpy?
If I have a ''string'' that should NOT be NULL terminated, is it good practice
to use "sizeof(str) - 1" or "sizeof(str)" when using a function like
strncpy?

Is this function bug-free?

void setname(char *name)
{
char null_buf[256];
char buf[256];
strncpy(buf, name, sizeof(buf)); /* don''t preserver NULL terminator */
strncpy(null_buf, name, sizeof(buf) - 1); /* preserve NULL terminator */
}

推荐答案



" Trevor" < TR **** @ spam.com> aécritdansle message de

news:yGmbc.62053

"Trevor" <tr****@spam.com> a écrit dans le message de
news:yGmbc.62053


K91.150753@attbi_s02 ...



K91.150753@attbi_s02...

Hi,
如果我有一个应该以NULL结尾的字符串,那么使用sizeof(str)是不错的做法。或sizeof(str) - 1或sizeof(str) - 1。当使用像strncpy这样的函数时?
如果我有一个不应该以NULL结尾的''字符串'',那么使用" sizeof(str) - 1"或sizeof(str)或sizeof(str)。使用像
strncpy这样的功能时?


当使用strncpy时,你处理的是字符串,或者通过指针访问
比使用数组更多。在这种情况下,sizeof并不是真的适合


为什么不使用strlen而不是sizeof运算符?

这个功能没有错误吗?


不,如果name为null,它可能会崩溃。

void setname(char * name)
{char / null char null_buf [256] ];
char buf [256];
strncpy(buf,name,sizeof(buf)); / *不要保留NULL终止符* /
strncpy(null_buf,name,sizeof(buf) - 1); / *保留NULL终止符* /
If I have a string that should be NULL terminated, is it good practice to
use "sizeof(str)" or "sizeof(str) - 1" when using a function like strncpy?
If I have a ''string'' that should NOT be NULL terminated, is it good practice to use "sizeof(str) - 1" or "sizeof(str)" when using a function like
strncpy?
When using strncpy, you deal with strings, perhaps much more accessed
through pointers than using arrays. In this case, sizeof isn''t really
appropriate.
Why don''t you use strlen instead of the sizeof operator ?
Is this function bug-free?
No, It probably crashes if name is null.
void setname(char *name)
{
char null_buf[256];
char buf[256];
strncpy(buf, name, sizeof(buf)); /* don''t preserver NULL terminator */
strncpy(null_buf, name, sizeof(buf) - 1); /* preserve NULL terminator */




如果名称较短,这两个字符都会保留终止空字符

而不是255个字符(包括\ 0)并且是零终止的。


如果你不想保留终止空字符,你会做

喜欢这样:


void setname(const char * name)

{

char * buf;

/*...*/

if(name!= NULL)

{

buf = malloc(strlen(name) );

strncpy(buf,name,strlen(name));

}

/*...*/

}


瑞吉斯



Both of these preserve the terminating null character if name is shorter
than 255 characters (including \0) and is zero-terminated.

If you don''t wan''t to keep the terminating null character, you''ll do
someting like this :

void setname(const char * name)
{
char * buf;
/*...*/
if (name!= NULL)
{
buf = malloc(strlen(name));
strncpy(buf, name, strlen(name));
}
/*...*/
}

Regis


错误,
void setname( const char * name)
{
char * buf;
/*...*/
if(name!= NULL)
{
buf = malloc(strlen(name));


if(buf!= NULL)

{

strncpy(buf,name,strlen(name));


}

}
/*...*/
void setname(const char * name)
{
char * buf;
/*...*/
if (name!= NULL)
{
buf = malloc(strlen(name));
if (buf != NULL)
{
strncpy(buf, name, strlen(name));
}
}
/*...*/



这篇关于sizeof(str)或sizeof(str) - 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆