函数原型:
long int strtol (const char* str, char** endptr, int base);
注意第二个形参是二级指针,如果定义char *stop,那么要出入&stop
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int main(void)
{
const char *str = "10 200000000000000000000000000000 30 -40 junk";
const char *begin = str;
printf("Parsing '%s', begin addr = %p:\n\n", begin, begin);
char *stop;
long i = strtol(begin, &stop, 10);
while (begin != stop)
{
printf("begin: %p\n", begin);
printf("stop: %p\n", stop);
printf("'%.*s' -> %ld\n", (int)(stop-begin), begin, i);
char *message = strerror(errno);
if (message != NULL) {
printf("%s\n\n", message);
errno = 0;
}
begin = stop;
i = strtol(begin, &a