CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

atoi array elements

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
barkerben



Joined: 19 Jan 2006
Posts: 22

View user's profile Send private message

atoi array elements
PostPosted: Fri Jan 20, 2006 7:38 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jan 20, 2006 8:05 pm     Reply with quote

Would this work?

atoi(*(&string+offset));


It might produce some ugly code though
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Mon Jan 23, 2006 8:47 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jan 23, 2006 8:47 pm     Reply with quote

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));
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group