C Parse String Into Two Ints With Strtol -


assume have program calculates result of multiplying 2 integers string. use strtol separate first part how separate second int? example, "12 5" give result of 60.

right code looks like:

    int multiply(const char *input) {         int result = 0;         char *second_int;         int = strtol(input_line, &second_int, 10);          result = * second_int;          return result; 

so right give error since converted first part of string integer. how convert leftover string integer? need strtol line? need cast it? i'm unsure of how go this.

strtol declaration following:

long int strtol(const char *nptr, char **endptr, int base); 

and man strtol:

if endptr not null, strtol() stores address of first invalid character in *endptr.

so can use value stored in *endptr start scanning value. example:

char *str="12 5"; char *end; printf("%ld\n", strtol(str, &end, 10)); printf("%ld\n", strtol(end, &end, 10)); 

will print:

12 5 

Comments