By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

strtol()

P: n/a
Hi,
I'am a little bit confused. I tried this snippet (below) and ( endptr !=
NULL ) is true everytime. I was looking to
http://www.mkssoftware.com/docs/man3/strtol.3.asp
and then to
http://www.cplusplus.com/ref/cstdlib/strtol.html
The second www specifies things like they are done - endptr is not NULL.
Is something wrong?

Best Regards,
Pavel Krcmar

---------------------------------
int main()
{
int i;
const char *z = "345";
char *endptr;

i = strtol( z, &endptr, 10 );
if ( endptr != NULL )
{
printf("endptr is not zero!\n");
printf("%s\n", endptr );
}
printf( "i=%i\n", i );
return 0;
}
---------------------------------
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Wed, 01 Sep 2004 10:42:25 +0200,
Pavel Krcmar <pa**********@siemens.com> wrote:
Hi,
I'am a little bit confused. I tried this snippet (below) and ( endptr !=
NULL ) is true everytime. I was looking to
http://www.mkssoftware.com/docs/man3/strtol.3.asp
and then to
http://www.cplusplus.com/ref/cstdlib/strtol.html
The second www specifies things like they are done - endptr is not NULL.
Is something wrong?
endptr simply gets set to point to the first character in the string
that is not part of the number. It will never be set to NULL, if
you pass NULL instead of an actual pointer to a char* then this
won't happen.

Best Regards,
Pavel Krcmar

---------------------------------
int main()
{
int i;
const char *z = "345";
char *endptr;

i = strtol( z, &endptr, 10 );
if ( endptr != NULL )
{
printf("endptr is not zero!\n");
printf("%s\n", endptr );
}
printf( "i=%i\n", i );
return 0;
}
---------------------------------


endptr will never equal NULL in that code, since it will be
set to point to the trailing '\0' in "345" by strtol.

--
Sam Holden
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.