spaces lead the number so I tried to make trim function

General discussion on mikroC.
Post Reply
Author
Message
Aoday
Posts: 86
Joined: 07 Mar 2005 13:45

spaces lead the number so I tried to make trim function

#1 Post by Aoday » 17 May 2005 07:05

when I used bytetostr function I found that the number converted and there a space before the number i.e if I want to convert number 5 it became " 5" if 55 became " 55" 255 became "255" you can see there is a space leads the number so I tried to make trim function to delete this spaces there is something error the function some time work and some times not working:

Code: Select all

char *Trim(char *Tstr) {
  unsigned short int a=0,i=0;
  char *temp;
  while(Tstr[a] == 0x20) {
   a++;
       };
while (Tstr[a]!=0){
  temp[i]=Tstr[a];
  i++;
  a++;
};
  temp[i]=0;
  return temp;
}
please some one help me

User avatar
rajkovic
mikroElektronika team
Posts: 694
Joined: 16 Aug 2004 12:40

Re: spaces lead the number so I tried to make trim function

#2 Post by rajkovic » 17 May 2005 08:13

Aoday wrote:when I used bytetostr function I found that the number converted and there a space before the number i.e if I want to convert number 5 it became " 5" if 55 became " 55" 255 became "255" you can see there is a space leads the number so I tried to make trim function to delete this spaces there is something error the function some time work and some times not working:

Code: Select all

char *Trim(char *Tstr) {
  unsigned short int a=0,i=0;
  char *temp;
  while(Tstr[a] == 0x20) {
   a++;
       };
while (Tstr[a]!=0){
  temp[i]=Tstr[a];
  i++;
  a++;
};
  temp[i]=0;
  return temp;
}
please some one help me

you have used uninitialized pointer temp so next line

Code: Select all

temp[i]=Tstr[a];
write sover RAM location (pointed by temp) with your temporary string.
Since temp is local variable upon every enetery it may have any value
so you are writting randomly trough RAM, this can write some impotrant globals or stack or even some registers so you get behaviour (sometimes working sometimes not)

This is one of solutions for your problem

Code: Select all

char* Trim(char *Tstr) {
  unsigned short int a=0,i=0;
  while(Tstr[a] == 0x20) {
   a++;
       };
while (Tstr[a]!=0){
  Tstr[i]=Tstr[a];
  i++;
  a++;
};
  Tstr[i] = 0;
  return Tstr;
}


char Str[10];
void main(){

    byteToStr(5,Str);
    Trim(str);

}
As you can see now function trims upon passed string so you do not need
additional RAM.

Also be carefull with passing strings,arrays,(pointers) to
convert functions size of that array(reserved memory) must be
at least:
4 bytes for bytetostr
5 bytes for shorttostr
6 bytes for wordtostr
7 bytes for inttostr
...

IN opposite conv. functions will overwrite some portion of RAM. And you have (sometimes working sometimes not) worst possible case.

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#3 Post by gambrose » 18 May 2005 17:00

rather than more the array why not have the function detect the start of the string i.e. not equal to 0x20 then return a pointer to this position as the new string position.

Code: Select all

char * Trim(char *toTrim)
{
  while(*toTrim == 0x20)
  {
    toTrim++;
  }
  return toTrim;
}
Graham Ambrose

Post Reply

Return to “mikroC General”