473,387 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

strtol()

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
1 2602
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Matthias Kluwe | last post by:
Hi! In C, my everyday usage of strtol looked like using char *end; strtol( text, &end, 10 ); to read an int/long and then checking end == ( text + strlen( text ) );
5
by: William Payne | last post by:
Hello, I am in the process of converting a C++ program to a C program. The user of the program is supposed to supply an integer on the command line and in the C++ version of the program I was using...
11
by: nrk | last post by:
Isn't: char s = "--4"; char *endptr; strtol(s, &endptr, 0); supposed to return 0 and set endptr to s? I have run into an implementation (not gcc, gcc does what I expect) that is returning...
10
by: Peter Dunker | last post by:
Hi, I will check a String which should contain a HEX value. I know that strtol is the right function for this job. But what happens when I will check a hex string with 8 bytes? That I can...
3
by: whisper | last post by:
Hello: I am trying to write code to read in a bunch of lines from stdin, each containing a (variable) number of integers and writing each integer in a separate line to stdout. I came up the...
2
by: Marlene Stebbins | last post by:
Suppose I'm using strtol() to convert a command line string to a number and I want to check that the input to strtol() is not non-numeric. strtol() returns zero if input is non-numeric, so I can...
14
by: Kristo | last post by:
Since there's no strtoi function in standard C, I've been searching the clc archives for the proper way to store the result of strtol to an int. My search has yielded conflicting results. About...
3
by: dstevel | last post by:
The signature for strtol is: strtol( const char*, char**, int) So.. if we start with a passed "const char*" (pointer to const char), then we can't create a non-const char pointer pointer to...
8
by: lovecreatesbea... | last post by:
Does this part of C code call and check strtol() correctly? port = strtol(argv, &endptr, 10); if (argv == endptr){ fprintf(stderr, "%s\n", "Invalid port number form"); return 1; } if (port ==...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.