Correct Usage of Strtol

Correct usage of strtol

You're almost there. temp itself will not be null, but it will point to a null character if the whole string is converted, so you need to dereference it:

if (*temp != '\0')

Basics of strtol?

The first argument is the string. It has to be passed in as a C string, so if you have a std::string use .c_str() first.

The second argument is optional, and specifies a char * to store a pointer to the character after the end of the number. This is useful when converting a string containing several integers, but if you don't need it, just set this argument to NULL.

The third argument is the radix (base) to convert. strtol can do anything from binary (base 2) to base 36. If you want strtol to pick the base automatically based on prefix, pass in 0.

So, the simplest usage would be

long l = strtol(input.c_str(), NULL, 0);

If you know you are getting decimal numbers:

long l = strtol(input.c_str(), NULL, 10);

strtol returns 0 if there are no convertible characters at the start of the string. If you want to check if strtol succeeded, use the middle argument:

const char *s = input.c_str();
char *t;
long l = strtol(s, &t, 10);
if(s == t) {
/* strtol failed */
}

If you're using C++11, use stol instead:

long l = stol(input);

Alternately, you can just use a stringstream, which has the advantage of being able to read many items with ease just like cin:

stringstream ss(input);
long l;
ss >> l;

How do I make sure that strtol() have returned successfully?

You need to pass a real pointer address if you want error checking, so you can distinguish 0 values arising from "0" and similar from 0 values arising from "pqr":

char *endptr;
errno = 0;
long result = strtol(str, &endptr, 10);
if (endptr == str)
{
// nothing parsed from the string, handle errors or exit
}
if ((result == LONG_MAX || result == LONG_MIN) && errno == ERANGE)
{
// out of range, handle or exit
}
// all went fine, go on

usage difference between atoi and strtol in c

The explication from man page:

The atoi() function converts the initial portion of the string pointed to by nptr to int. The behavior is the same as:

strtol(nptr, NULL, 10);

except that atoi() does not detect errors.
The atol() and atoll() functions behave the same as atoi(), except that they convert the initial portion of the string to their return type of long or long long.

For more infos, you can see the difference of two functions in this topic atoi - strtol

C Parsing String Function Argument Using strtol

There is no need to dereference the input parameter. If you simply drop the line

char *input = input_line; 

(which isn't the right way to dereference it anyways), the code will work. You are passing sum a pointer to char, which is exactly what the first argument to strol should be.

A simple test:

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

int sum(const char *input){
int i; // store sum in here
char *rest; // used as second argument in strtol so that first int can be added to i
i = strtol(input, &rest, 10); // set first int in string to i
i += strtol(rest, &rest, 10); // add first and second int
return i;
}

int main(void){
char* nums = "23 42";
printf("%d\n",sum(nums));
return 0;
}

It prints 65 as expected.

As far as the mechanics of dereferencing goes: If for some reason you really wanted to dereference the pointer passed to sum you would do something like this (inside sum):

char ch; // dereferencing a char* yields a char
ch = *input; // * is the dereference operator

Now ch would hold the first character in the input string. There is no reason at all to pass an individual char to strol hence such dereferencing is in this case pointless -- although there are of course valid reasons for sometimes in the body of a function dereferencing a pointer which is passed to that function.



Related Topics



Leave a reply



Submit