In order of preference
strtol() - does conversion and will report numeric overflow / underflow and tell you how much data was processed
sscanf() - like strtol() without the over/under flow
atoi() - pretty useless atoi("0") and atoi("hello") may both return 0
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
Maybe something like this is what you are looking for
int main (void)
{
int i, j ;
char num_string[] = "1 234 23 45" ;
char tmp [2] = {'\0', '\0'} ;
int length = strlen (num_string) ;
int* values = (int*) malloc (sizeof (int) * length) ;
for ( i = 0, j = 0; i < length; ++i)
{
if ( isdigit (num_string[i] ) )
{
tmp [0] = num_string [i] ;
values [j++] = atoi (tmp) ;
}
}
printf ("\nThe string: %s", num_string) ;
printf ("\nThe integer array which results is: ") ;
for ( i = 0; i < j; ++i)
printf (" %d ", values[i]) ;
return 0 ;
}
My output is:The string: 1 234 23 45
The integer array which results is: 1 2 3 4 2 3 4 5
Press ENTER to continue.
Hope it helped , bye.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734