strtol()

P

Pavel Krcmar

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;
}
---------------------------------
 
S

Sam Holden

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top