Hi all,
New to this forum, so not sure if this is the place to ask questions or not.

I'm confused as to how the os_RealToStr() function is used, since it seems to take chars instead of ints for its last 3 arguments, yet any char I pass to it results in an error saying that the "argument type is not compatible with formal parameter."

Here is my code (tried to make some float to string function). I feel like this is just some simple fix but I can't figure out what's wrong...

Code:

char *floatToStr(float arg)
{
    char *result;
    const real_t arg2 = os_FloatToReal(arg);
    os_RealToStr(result, arg2, '0', '0', '0');
    return result;
}
Documentation, as provided by tice.h:

Code:
/** This converts a ti-float to a ti-ascii string.
 *
 * @param result Zero terminated string copied to this address
 * @param arg Real to convert
 * @param maxLength
 *  <=0: use default max length (14)                            <br>
 *  >0:  max length of result, minimum of 6
 * @param mode:
 *  0: Use current mode for everything (digits ignored)         <br>
 *  1: Normal mode                                              <br>
 *  2: Sci mode                                                 <br>
 *  3: Eng mode                                                 <br>
 *  >4: Use current Normal/Sci/Eng mode (digits still used)     <br>
 * @param digits
 *  -1:  Float mode                                             <br>
 *  0-9: Fix # mode                                             <br>
 * @returns Length of result
 */
int os_RealToStr(char *result, const real_t *arg, char maxLength, char mode, char digits);


In this case, char means an 8-bit integer, not a literal character.
The use of char as a type for small numbers is rather confusing; I ask Mateo why int8_t or uint8_t wasn't used instead.
Still getting the same error, whether I use

Code:
os_RealToStr(result, arg2, 0, 0, 0);

or

Code:
os_RealToStr(result, arg2, (char)(0), (char)(0), (char)(0));
As per the documentation; the thing that isn't the issue... it's just C... You cannot return a local like that in C; nor can you send a pointer without any allocation on it. Also, the second argument expects a pointer, not an object. Hope this helps! Welcome to Cemetech Smile

This should do the trick:


Code:

char result[15];

char *floatToStr(float arg)
{
    const real_t arg2 = os_FloatToReal(arg);
    os_RealToStr(result, &arg2, 0, 0, 0);
    return result;
}


Or you can use malloc() to dynamically allocate a string if you need Smile

oldmud0 wrote:
The use of char as a type for small numbers is rather confusing; I ask Mateo why int8_t or uint8_t wasn't used instead.

Oops thank you Smile

Runer112 wrote:
In this case, char means an 8-bit integer, not a literal character.

Also note that the code still would have compiled fine even if he had used literal characters Razz
Alright, so it seems I've run into another issue. I'm getting an error saying that "An array is not a legal value" when trying to use the function:


Code:

float a = 5;
char text[15];
text = floatToStr(a);
sw4nky wrote:
Alright, so it seems I've run into another issue. I'm getting an error saying that "An array is not a legal value" when trying to use the function:


Code:

float a = 5;
char text[15];
text = floatToStr(a);

Again, you may want to learn how to code in C a little first Razz This is because it returns a pointer to the array, which is stored in result. Wink

You can fix this by doing:


Code:

float a = 5;
char *text;
text = floatToStr(a);


Or by passing the array to the function:


Code:

float a = 5;
char text[15];
floatToStr(a, text);

---
void floatToStr(float arg, char *array)
{
    const real_t arg2 = os_FloatToReal(arg);
    os_RealToStr(array, &arg2, 0, 0, 0);
}

MateoConLechuga wrote:
sw4nky wrote:
Alright, so it seems I've run into another issue. I'm getting an error saying that "An array is not a legal value" when trying to use the function:


Code:

float a = 5;
char text[15];
text = floatToStr(a);

Again, you may want to learn how to code in C a little first Razz This is because it returns a pointer to the array, which is stored in result. Wink

You can fix this by doing:


Code:

float a = 5;
char *text;
text = floatToStr(a);


Or by passing the array to the function:


Code:

float a = 5;
char text[15];
floatToStr(a, text);

---
void floatToStr(float arg, char *array)
{
    const real_t arg2 = os_FloatToReal(arg);
    os_RealToStr(array, &arg2, 0, 0, 0);
}


Code works now, thanks for the help! Very Happy
I definitely need to brush up on C though, especially since I don't have a full understanding of pointers yet.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
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

 

Advertisement