I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
Index »

converting from char * to int on result from strtok

graceR\′s Photo
18 Mar 11, 7:48PM
(1 reply)
converting from char * to int on result from strtok
input is something like "word-2" How do I convert from char pointer to int ?

  1. char *tmp_result = NULL;
  2. char standard = "";
  3. int version = 0;
  4. tmp_result = strtok( argv[1], delims );
  5. printf( "token standard is "%s"
    ", tmp_result );
  6. standard = *tmp_result;
  7. if( tmp_result != NULL ) {
  8. tmp_result = strtok( NULL, delims );
  9. printf( "token tmp_result is "%s"
    ", tmp_result ); <===== prints "2" as it should be
  10. version = *tmp_result;
  11. printf( "token as_version is "%d"
    ", as_version ); <===== prints "50" after assignment
}
john\′s Photo
19 Mar 11, 11:31PM
A char pointer refers to the start memory address of a string. You can't directly transfer to an int.

However, it your trying to convert a string (ie "123.42") then you can use atoi for integers or atof for real numbers
Currently you need to be logged in to leave a message.