Char Array questions

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
eckcotech
Posts: 70
Joined: 03 Apr 2011 21:13

Char Array questions

#1 Post by eckcotech » 03 Apr 2011 21:24

Hey all,

I have a small project that is pulling in an ADC value and converting it to a char string value with 8 bits....

Code: Select all

unsigned int myadc;
char adctxt[];
float aveadc;

myadc = ADC_Read(0);
aveadc = ((myadc * .6162) - 62.691);
FloatToStr(aveadc,adctxt);
This piece works great and is returning good results but they are formatted similar to this....

result = 51.58397

What i would like to do is get rid of anything beyond 2 decimal places and pad the number before the decimal to 3 places. something like this....

result = 051.58

I tried to do that by cycling through the array with the result in it and making decisions based on where the decimal landed. unfortunately, I have had no luck getting this to work as expected. Here's the code......

Code: Select all

if(adctxt[0]="."){
            retstr[0] = "0";
            retstr[1] = "0";
            retstr[2] = "0";
            retstr[3] = ".";
            retstr[4] = adctxt[1];
            retstr[5] = adctxt[2];
        }

        if(adctxt[1]="."){
            retstr[0] = "0";
            retstr[1] = "0";
            retstr[2] = adctxt[0];
            retstr[3] = ".";
            retstr[4] = adctxt[1];
            retstr[5] = adctxt[2];
        }
        
        if(adctxt[2]="."){
            retstr[0] = "0";
            retstr[1] = adctxt[0];
            retstr[2] = adctxt[1];
            retstr[3] = ".";
            retstr[4] = adctxt[2];
            retstr[5] = adctxt[3];
        }
        
        if(adctxt[3]="."){
            retstr[0] = adctxt[0];
            retstr[1] = adctxt[1];
            retstr[2] = adctxt[2];
            retstr[3] = ".";
            retstr[4] = adctxt[3];
            retstr[5] = adctxt[4];
        }

when I run this, it seems to print out garbage via the UART1_Write_Text(retstr).

Any ideas on what I might be doing wrong here??

KaranSoin
Posts: 130
Joined: 07 May 2010 22:27
Location: Melbourne, Australia

Re: Char Array questions

#2 Post by KaranSoin » 04 Apr 2011 12:10

use sprintf() , it is designed for problems like this. The reason you are getting garbage is because size of adctxt[] is undefined, you are most likely overwriting the NULL terminating character at the end of the string.

Regards

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: Char Array questions

#3 Post by Mince-n-Tatties » 04 Apr 2011 12:13

look up C string tools

memchr, you could use this to find the location of the decimal point in the array and work around that.
Best Regards

Mince

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: Char Array questions

#4 Post by Dany » 05 Apr 2011 07:53

Code: Select all

char adctxt[];
Should this variable not be something like

Code: Select all

char adctxt[23];
to be capable of holding the string representation of a floating variable?
I think (but again I am no C guru), that

Code: Select all

char adctxt[];
only declared a pointer to a string and does not reservate space for the string itself. :shock:
All goes well until other some other variable is overwritten with the string contents.

Things like this should be taken up in the list "pitfalls for C programmers", if that existed.

This is only a side remark, it has no relation to the thread subject.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: Char Array questions

#5 Post by Mince-n-Tatties » 05 Apr 2011 09:29

Hi Dany,
Dany wrote:

Code: Select all

char adctxt[];
Should this variable not be something like

Code: Select all

char adctxt[23];
to be capable of holding the string representation of a floating variable?
[15] for the specific conversion library used.
Dany wrote: Things like this should be taken up in the list "pitfalls for C programmers", if that existed.
mmm in the case of this exact topic side remark, this information already exist in the form of the user help manual "F1"
Best Regards

Mince

Post Reply

Return to “mikroC PRO for PIC General”