|
|
View previous topic :: View next topic |
Author |
Message |
mark7930SRL Guest
|
problem with itoa and string |
Posted: Thu Aug 21, 2008 8:26 am |
|
|
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
|
|
Posted: Thu Aug 21, 2008 8:31 am |
|
|
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
|
|
Posted: Thu Aug 21, 2008 8:44 am |
|
|
Replace "can", with "can't" on the penultimate line.....
Best Wishes |
|
|
RayJones
Joined: 20 Aug 2008 Posts: 30 Location: Melbourne, Australia
|
|
Posted: Fri Aug 22, 2008 5:31 am |
|
|
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. |
|
|
|
|
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
|