对strlen函数为NULL [英] NULL to strlen function

查看:222
本文介绍了对strlen函数为NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我在Microsoft Visual c + + 6.0中尝试了以下程序单元,并且

程序导致了意外行为,


#include< stdio.h>

#include< string.h>


int main()

{

char * p;

p = NULL;

printf("%d \ n",strlen(p) ));

}


是否strlen函数无法处理NULL指针?

Hi Everyone,

I tried the following program unit in Microsoft Visual c++ 6.0 and the
program caused unexpected behavior,

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

int main()
{
char *p;
p = NULL;
printf("%d\n",strlen(p));
}

Is it that strlen function can''t handle NULL pointers?

推荐答案



sam _... @ yahoo.co.in写道:

sam_...@yahoo.co.in wrote:

是吗strlen函数无法处理NULL指针?
Is it that strlen function can''t handle NULL pointers?



Strlen期望一个字符串作为参数。 NULL指针是一个字符串吗?

(提示:这是一个修辞问题)。

Strlen expects a string as an argument. Is a NULL pointer a string?
(Hint: this is a rhetorical question).


sa ***** @ yahoo.co.in 说:

嗨每个人,

我在Microsoft Visual c + + 6.0中尝试了以下程序单元,并且

程序导致了意外行为,
Hi Everyone,

I tried the following program unit in Microsoft Visual c++ 6.0 and the
program caused unexpected behavior,



未定义的行为。

Undefined behaviour.


#include< stdio.h>

#include< string.h>


int main()

{

char * p;

p = NULL;

printf("%d \ nn",strlen(p));

}


这是strlen函数可以'不处理NULL指针?
#include <stdio.h>
#include <string.h>

int main()
{
char *p;
p = NULL;
printf("%d\n",strlen(p));
}

Is it that strlen function can''t handle NULL pointers?



或者是strlen返回size_t而你的格式说明符需要int吗?


无法分辨出这些中的哪一个两个产生未定义的行为

你注意到了。


任何好的C书都会解释strlen的语义。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

Or is it that strlen returns size_t and your format specifier requires int?

It is impossible to tell which of these two produces the undefined behaviour
you have noticed.

Any good C book will explain the semantics of strlen.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.




< sa * ****@yahoo.co.in在留言中写道

新闻:11 ********************* @ o58g2000hsb.googlegro ups。 com ...

<sa*****@yahoo.co.inwrote in message
news:11*********************@o58g2000hsb.googlegro ups.com...

大家好,

我在Microsoft Visual c + + 6.0和
程序导致意外行为,


#include< stdio.h>

#include< string.h>


int main()

{

char * p;

p = NULL;

printf("%d \ nn",strlen(p));

}


这是strlen函数不能吗?处理NULL指针?
Hi Everyone,

I tried the following program unit in Microsoft Visual c++ 6.0 and the
program caused unexpected behavior,

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

int main()
{
char *p;
p = NULL;
printf("%d\n",strlen(p));
}

Is it that strlen function can''t handle NULL pointers?



我的解释是定义strlen不能

正确处理NULL指针。


退货


strlen函数返回

之前终止null

字符的字符数。

My interpretation would be by definition strlen cannot
properly handle a NULL pointer.

Returns

The strlen function returns the number of characters that precede the
terminating null
character.


这篇关于对strlen函数为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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