strtol库函数 [英] strtol library function

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

问题描述

另外两个问题......


当strtol的第一个参数为NULL时是否定义了行为?


如果字符串包含只有数字,是第二个参数(假设

它不是NULL)保证被设置为指向''\0''的指针?

谢谢。


-

David Scarlett


dscarlett @ _ _ _ _ _ _ _ _

_ _ _ _ _ optusnet.com.au

解决方案

" David Scarlett" <螺** @ my.signature>在留言中写道

新闻:Xn *********************** @ 211.29.133.50 ...

另外两个问题......

当strtol的第一个参数是NULL时,是否定义了行为?


No.

如果字符串只包含数字,则第二个参数(假设它不是NULL)保证是设置为指向''\0''指针的指针?



第二个参数由value作为指针指针传递。这使得

对于它可能设置的内容毫无意义。但是成功返回时指向它的指针应该指定终止NUL,

在你描述的情况下。我认为这就是你真正的意思。


PJ Plauger

Dinkumware,Ltd。
http://www.dinkumware.com





David Scarlett写道:

另外两个问题......

当strtol的第一个参数为NULL时是否定义了行为?




第一个agrument必须是一个代表指向

a字符串的值。没有为函数定义NULL值。


我有关于errno的问题。鉴于以下代码,即
只是检测是否存在适当的转换,是否需要使用LONG_MIN或LONG_MAX测试

或者可以使用测试

errno == ERANGE?


#include< stdio.h>

#include< limits.h>

#include< errno.h>

#include< stdlib.h>


int main(void)

{

char * str =" -123456789999999999999",* s;

long num = 0;


errno = 0;

num = strtol(str,& s,10);

if(s == str || * s!=''\ 0''||

((errno == ERANGE)&&(num == LONG_MAX || num == LONG_MIN)))

printf(" \%s \未转换为长值\ n,str);

else

printf(" \" %s \" =%ld \ n",str,num);

返回0;

}

-

Al Bowers

Tampa,Fl USA
mailto: xa******@myrapidsys.com (删除x发送电子邮件)
http://www.geocities.com/abowers822/


" Al Bowers" < xa的****** @ rapidsys.com>在留言中写道

新闻:2g ************ @ uni-berlin.de ...

我有一个关于errno的问题。鉴于以下代码,它只是检测是否存在适当的转换,是否需要使用LONG_MIN或LONG_MAX进行测试,或者可以使用测试
errno == ERANGE?

#include< stdio.h>
#include< limits.h>
#include< errno.h>
#include< stdlib.h> ;

int main(无效)
{
char * str =" -123456789999999999999",* s;
long num = 0;

errno = 0;
num = strtol(str,& s,10);
if(s == str || * s!=''\ 0''||
((errno == ERANGE)&&(num == LONG_MAX || num == LONG_MIN)))
printf(" \"%s \"不转换为一个很长的值\ n,str);

printf(" \"%s \" =%ld \ n",str,num);
返回0;
}




errno的测试应该足够了。


PJ Plauger

Dinku mware,Ltd。
http://www.dinkumware.com


Another two questions...

Is behaviour defined when the first argument of strtol is NULL?

And if the string contains only digits, is the 2nd argument (assuming
it wasn''t NULL) guaranteed to be set to a pointer to a pointer to ''\0''?
Thanks.

--
David Scarlett

dscarlett@_ _ _ _ _ _ _ _
_ _ _ _ _ optusnet.com.au

解决方案

"David Scarlett" <lo**@my.signature> wrote in message
news:Xn***********************@211.29.133.50...

Another two questions...

Is behaviour defined when the first argument of strtol is NULL?
No.
And if the string contains only digits, is the 2nd argument (assuming
it wasn''t NULL) guaranteed to be set to a pointer to a pointer to ''\0''?



The second argument is passed by value as a pointer to pointer. It makes
no sense to talke about what it might get set to. But the pointer it
points at on a successful return should designate the terminating NUL,
in the case you describe. I think that''s what you really meant.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com




David Scarlett wrote:

Another two questions...

Is behaviour defined when the first argument of strtol is NULL?



The first agrument must be a value that represents a pointer to
a string. A value of NULL is not defined for the function.

I have a question regarding errno. Given the following code, which
simply detects if there was a proper coversion or not, is the test
using LONG_MIN or LONG_MAX neccessary or can one use use the test
errno == ERANGE?

#include <stdio.h>
#include <limits.h>
#include <errno.h>
#include <stdlib.h>

int main(void)
{
char *str = "-123456789999999999999", *s;
long num = 0;

errno = 0;
num = strtol(str,&s,10);
if(s == str || *s != ''\0'' ||
((errno == ERANGE) && (num == LONG_MAX || num == LONG_MIN)))
printf("\"%s\" does not convert to a long value\n",str);
else
printf("\"%s\" = %ld\n",str,num);
return 0;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


"Al Bowers" <xa******@rapidsys.com> wrote in message
news:2g************@uni-berlin.de...

I have a question regarding errno. Given the following code, which
simply detects if there was a proper coversion or not, is the test
using LONG_MIN or LONG_MAX neccessary or can one use use the test
errno == ERANGE?

#include <stdio.h>
#include <limits.h>
#include <errno.h>
#include <stdlib.h>

int main(void)
{
char *str = "-123456789999999999999", *s;
long num = 0;

errno = 0;
num = strtol(str,&s,10);
if(s == str || *s != ''\0'' ||
((errno == ERANGE) && (num == LONG_MAX || num == LONG_MIN)))
printf("\"%s\" does not convert to a long value\n",str);
else
printf("\"%s\" = %ld\n",str,num);
return 0;
}



The test of errno should suffice.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


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

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