Four Lines Implementing String to Long

Four Lines Implementing String to Long
1  int  string2long( char *  str,  long *  rtv)
2  {
3       for ( int  i  =  strlen(str), j  =   * rtv  =   0 * str  >=   ' 0 '   &&   * str  <=   ' 9 ' ; i -- , str ++ ){
4           * rtv  +=  ( long )(( * str  -   ' 0 ' ) * (pow( 10 , i - 1 )));
5      }
6       return   * str  ==   ' \0 '   ?   1  :  0 ;
7  }
Please pay attention to j = *rtv = 0;
if there is no "j = ", *rtv will be redefined as int *rtv after ", " and address of rtv will be 0xcccccc.
So, inorder to set value of rtv in if sentence, just use a non-usage variable j.

to invoke the function in this way:

long rtv;
if(string2long("321", &rtv)) printf("rtv = %ld\n", rtv);
else puts("failure");


你可能感兴趣的:(Four Lines Implementing String to Long)