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

problem with itoa and string

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







problem with itoa and string
PostPosted: Thu Aug 21, 2008 8:26 am     Reply with quote

Hi, i'm a student and I'm learnig C .I have this code

char string[5];
int16 value;

suppose that value is 14578.

itoa((int32)value,10, string);

String now becomes "14578".

How can I change one digit of string. ie: I want to change into 145C8 without subtraction. Can I Do it?

I use this code but is not good:

string[3]="C";

compiler say " Bad expression syntax "

thank's at all...
Ttelmah
Guest







PostPosted: Thu Aug 21, 2008 8:31 am     Reply with quote

Start by making your buffer bigger....
Remember that a 'string', has a terminating '\0', and there needs to be space for this as well. As it stands, you will be overwriting the next character in memory, with potentially disastrous result...
The problem with your replacement, is that "C" (with double inverted commas), is itself a _string_, not a single character. You can transfer a string like this. You need to use the single inverted commas, to say the 'character' C. So:

string[3]='C';

Best Wishes
Ttelmah
Guest







PostPosted: Thu Aug 21, 2008 8:44 am     Reply with quote

Replace "can", with "can't" on the penultimate line.....

Best Wishes
RayJones



Joined: 20 Aug 2008
Posts: 30
Location: Melbourne, Australia

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

PostPosted: Fri Aug 22, 2008 5:31 am     Reply with quote

Yep, as ttelmah said, you must cater for the hidden trailing '\0' so you need 6 bytes to hold the UNSIGNED integer, 7 if you venture into signed territory.

As an additional item, you can initialise a a string using:

char string[] = "a string";

ie don't worry about defining a size, the complier sorts that out, and includes the trailing zero for you.
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