Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.
// if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ... if(*ptr != '\n' && *ptr != '0円') {
Note: Code lacks robust error detection/handling in "string to
long
toint
" conversion. Could use you ownstrtoi()
or this this or roll your own conversion like below:// return true if error bool parse_int(const char *token, int *value_int) { char *endptr; errno = 0; long value_long = strtol(token, &endptr, /* base */ 10); if (token == endptr) { // no conversion *value_int = 0; return true; } #if INT_MAX < LONG_MAX if (value_long > INT_MAX) { *value_int = INT_MAX; errno = ERANGE; return true; } #endif #if INT_MIN > LONG_MIN if (value_long < INT_MIN) { *value_int = INT_MIN; errno = ERANGE; return true; } #endif *value_int = (int) value_long; if (errno) { return true; // outside long range or other ID error } if (*endptr != '\n' && *endptr != '0円') { // junk at the end return true; } return false; }
By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.
// long node_value; .... 12 lines while(token != NULL){ long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/ ... add_node(node_value);
Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.
// if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ... if(*ptr != '\n' && *ptr != '0円') {
Note: Code lacks robust error detection/handling in "string to
long
toint
" conversion. Could use you ownstrtoi()
or this or roll your own conversion like below:// return true if error bool parse_int(const char *token, int *value_int) { char *endptr; errno = 0; long value_long = strtol(token, &endptr, /* base */ 10); if (token == endptr) { // no conversion *value_int = 0; return true; } #if INT_MAX < LONG_MAX if (value_long > INT_MAX) { *value_int = INT_MAX; errno = ERANGE; return true; } #endif #if INT_MIN > LONG_MIN if (value_long < INT_MIN) { *value_int = INT_MIN; errno = ERANGE; return true; } #endif *value_int = (int) value_long; if (errno) { return true; // outside long range or other ID error } if (*endptr != '\n' && *endptr != '0円') { // junk at the end return true; } return false; }
By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.
// long node_value; .... 12 lines while(token != NULL){ long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/ ... add_node(node_value);
Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.
// if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ... if(*ptr != '\n' && *ptr != '0円') {
Note: Code lacks robust error detection/handling in "string to
long
toint
" conversion. Could use you ownstrtoi()
or this or roll your own conversion like below:// return true if error bool parse_int(const char *token, int *value_int) { char *endptr; errno = 0; long value_long = strtol(token, &endptr, /* base */ 10); if (token == endptr) { // no conversion *value_int = 0; return true; } #if INT_MAX < LONG_MAX if (value_long > INT_MAX) { *value_int = INT_MAX; errno = ERANGE; return true; } #endif #if INT_MIN > LONG_MIN if (value_long < INT_MIN) { *value_int = INT_MIN; errno = ERANGE; return true; } #endif *value_int = (int) value_long; if (errno) { return true; // outside long range or other ID error } if (*endptr != '\n' && *endptr != '0円') { // junk at the end return true; } return false; }
By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.
// long node_value; .... 12 lines while(token != NULL){ long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/ ... add_node(node_value);
Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.
// if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ... if(*ptr != '\n' && *ptr != '0円') {
Note: Code lacks robust error detection/handling in "string to
long
toint
" conversion. Could use you ownstrtoi()
or this or roll your own conversion like below:// return true if error bool parse_int(const char *token, int *value_int) { char *endptr; errno = 0; long value_long = strtol(token, &endptr, /* base */ 10); if (token == endptr) { // no conversion *value_int = 0; return true; } #if INT_MAX < LONG_MAX if (value_long > INT_MAX) { *value_int = INT_MAX; errno = ERANGE; return true; } #endif #if INT_MIN > LONG_MIN if (value_long < INT_MIN) { *value_int = INT_MIN; errno = ERANGE; return true; } #endif *value_int = (int) value_long; if (errno) { return true; // outside long range or other ID error } if (*endptr != '\n' ||&& *endptr != '0円') { // junk at the end return true; } return false; }
By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.
// long node_value; .... 12 lines while(token != NULL){ long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/ ... add_node(node_value);
Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.
// if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ... if(*ptr != '\n' && *ptr != '0円') {
Note: Code lacks robust error detection/handling in "string to
long
toint
" conversion. Could use you ownstrtoi()
or this or roll your own conversion like below:// return true if error bool parse_int(const char *token, int *value_int) { char *endptr; errno = 0; long value_long = strtol(token, &endptr, /* base */ 10); if (token == endptr) { // no conversion *value_int = 0; return true; } #if INT_MAX < LONG_MAX if (value_long > INT_MAX) { *value_int = INT_MAX; errno = ERANGE; return true; } #endif #if INT_MIN > LONG_MIN if (value_long < INT_MIN) { *value_int = INT_MIN; errno = ERANGE; return true; } #endif *value_int = (int) value_long; if (errno) { return true; // outside long range or other ID error } if (*endptr != '\n' || *endptr != '0円') { // junk at the end return true; } return false; }
By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.
// long node_value; .... 12 lines while(token != NULL){ long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/ ... add_node(node_value);
Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.
// if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ... if(*ptr != '\n' && *ptr != '0円') {
Note: Code lacks robust error detection/handling in "string to
long
toint
" conversion. Could use you ownstrtoi()
or this or roll your own conversion like below:// return true if error bool parse_int(const char *token, int *value_int) { char *endptr; errno = 0; long value_long = strtol(token, &endptr, /* base */ 10); if (token == endptr) { // no conversion *value_int = 0; return true; } #if INT_MAX < LONG_MAX if (value_long > INT_MAX) { *value_int = INT_MAX; errno = ERANGE; return true; } #endif #if INT_MIN > LONG_MIN if (value_long < INT_MIN) { *value_int = INT_MIN; errno = ERANGE; return true; } #endif *value_int = (int) value_long; if (errno) { return true; // outside long range or other ID error } if (*endptr != '\n' && *endptr != '0円') { // junk at the end return true; } return false; }
By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.
// long node_value; .... 12 lines while(token != NULL){ long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/ ... add_node(node_value);
Only a bit to a bit to add beyond @pacmaninbw answer:
Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.
// if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ... if(*ptr != '\n' && *ptr != '0円') {
Note: Code lacks robust error detection/handling in "string to
long
toint
" conversion. Could use you ownstrtoi()
or this or roll your own conversion like below:// return true if error bool parse_int(const char *token, int *value_int) { char *endptr; errno = 0; long value_long = strtol(token, &endptr, /* base */ 10); if (token == endptr) { // no conversion *value_int = 0; return true; } #if INT_MAX < LONG_MAX if (value_long > INT_MAX) { *value_int = INT_MAX; errno = ERANGE; return true; } #endif #if INT_MIN > LONG_MIN if (value_long < INT_MIN) { *value_int = INT_MIN; errno = ERANGE; return true; } #endif *value_int = (int) value_long; if (errno) { return true; // outside long range or other ID error } if (*endptr != '\n' || *endptr != '0円') { // junk at the end *value_int = 0; return true; } return false; }
By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.
// long node_value; .... 12 lines while(token != NULL){ long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/ ... add_node(node_value);
Only a bit to a bit to add beyond @pacmaninbw answer:
Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.
// if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ... if(*ptr != '\n' && *ptr != '0円') {
Note: Code lacks robust error detection/handling in "string to
long
toint
" conversion. Could use you ownstrtoi()
or this or roll your own conversion like below:// return true if error bool parse_int(const char *token, int *value_int) { char *endptr; errno = 0; long value_long = strtol(token, &endptr, /* base */ 10); if (token == endptr) { // no conversion *value_int = 0; return true; } if (value_long > INT_MAX) { *value_int = INT_MAX; errno = ERANGE; return true; } if (value_long < INT_MIN) { *value_int = INT_MIN; errno = ERANGE; return true; } *value_int = (int) value_long; if (errno) { return true; // outside long range or other ID error } if (*endptr != '\n' || *endptr != '0円') { // junk at the end *value_int = 0; return true; } return false; }
By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.
// long node_value; .... 12 lines while(token != NULL){ long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/ ... add_node(node_value);
Only a bit to add beyond @pacmaninbw answer:
Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.
// if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ... if(*ptr != '\n' && *ptr != '0円') {
Note: Code lacks robust error detection/handling in "string to
long
toint
" conversion. Could use you ownstrtoi()
or this or roll your own conversion like below:// return true if error bool parse_int(const char *token, int *value_int) { char *endptr; errno = 0; long value_long = strtol(token, &endptr, /* base */ 10); if (token == endptr) { // no conversion *value_int = 0; return true; } #if INT_MAX < LONG_MAX if (value_long > INT_MAX) { *value_int = INT_MAX; errno = ERANGE; return true; } #endif #if INT_MIN > LONG_MIN if (value_long < INT_MIN) { *value_int = INT_MIN; errno = ERANGE; return true; } #endif *value_int = (int) value_long; if (errno) { return true; // outside long range or other ID error } if (*endptr != '\n' || *endptr != '0円') { // junk at the end return true; } return false; }
By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.
// long node_value; .... 12 lines while(token != NULL){ long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/ ... add_node(node_value);