View previous topic :: View next topic |
Author |
Message |
barkerben
Joined: 19 Jan 2006 Posts: 22
|
atoi array elements |
Posted: Fri Jan 20, 2006 7:38 pm |
|
|
Hello,
I'm pretty sure of the answer to this one...
If I have an array of chars containing "123" that I want to convert to an integer value, I can use atoi.
If however I just want to process some of that array - for instance if I wanted to extract 23 - is there a way of specifying atoi to work only on certain elements of the array? I suspect I would first have to extract the desired elements into a new array, then use atoi on that...?
Cheers |
|
|
Eugeneo
Joined: 30 Aug 2005 Posts: 155 Location: Calgary, AB
|
|
Posted: Fri Jan 20, 2006 8:05 pm |
|
|
Would this work?
atoi(*(&string+offset));
It might produce some ugly code though |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Mon Jan 23, 2006 8:47 am |
|
|
I think that would work, if you have the \0 at the end that defines a string.
But if you have "1234\0" and try to pull out 23, you'l have problems.
I would make another array. Fill it with all \0 's and the memcpy the
charaters you want over that. That way you'll get the \0
(provided your array is +1 the max digits you'l use. |
|
|
Eugeneo
Joined: 30 Aug 2005 Posts: 155 Location: Calgary, AB
|
|
Posted: Mon Jan 23, 2006 8:47 pm |
|
|
treitmey wrote: |
I would make another array. Fill it with all \0 's and the memcpy the
charaters you want over that. That way you'll get the \0
(provided your array is +1 the max digits you'l use. |
Right, but do you need a memcopy
So use
where:
off_set is the start
end_string is the end
code:
string[end_string+1]=null;
=atoi(*(&string+off_set)); |
|
|
|