0

I'm trying to convert a float32 to char[255] in Ansys Scade Suite. I'm using a custom function here that's taking in a float32 input and outputting a char array of 255. It can be seen below.

#include <stdlib.h>
#include "kcg_types.h"

kcg_char OP_StringToFloat(
    /* string/ */  const kcg_float *d
 )
{

kcg_char a[255];
sprintf(a, "%f", d);
return a;
}

kcg_char is a char.

For some reason the custom function is crashing the program leaving an untraceable System Error (SYSERR). Can anyone who uses Scade Suite help me out? This is model based programming btw. Here's how the connection look. Model

  • 4
    You cannot return a local array from a function, because it will no longer exist. You must either pass a buffer to the function, or allocate memory for one. Please see [Function returning address of local variable error in C](https://stackoverflow.com/questions/22288871/function-returning-address-of-local-variable-error-in-c). – Weather Vane Aug 03 '21 at 19:53
  • 1
    Also, your function receives a parameter of type pointer-to-float and then you pass that pointer-to-float to sprintf() but sprintf() wants a float, not a pointer-to-float. – kkrambo Aug 03 '21 at 19:55
  • 1
    Doesn't the compiler issue some warnings? Parameter mismatch for `sprintf` or returning address of local object? Can you increase warning level? – Gerhardh Aug 03 '21 at 19:58
  • @kkrambo so would it be better to make it a reference or is there a way to refer to it as a type pointer to float in sprintf? – Justin Young Aug 03 '21 at 20:30
  • 4
    Dereferencing a pointer is `sprintf(..., *d)`. But your main problem is returning the address of a no-longer-live stack variable and the return type being a single char instead of a string. The return type should be `kcg_char *`. What to do about `a` is not so clear. (see link in first comment) If the caller immediately copies the resulting string, you could also get away with declaring `a` as `static` as a quick and horrible fix. –  Aug 03 '21 at 20:57
  • Does that even compile? You are returning a `kcg_char*` from a function declared as returning `kcg_char`. What does the comment `/* string/ */` signify? – Clifford Aug 04 '21 at 11:10
  • At https://github.com/openETCS/srcAndBinary/blob/master/API_for_ERSa/KCG-ERSA/balise/kcg_types.h there is not such type `kcg_float` (only `kcg_real` which is an alis for `double`). – Clifford Aug 04 '21 at 11:16
  • For the purposes of asking a question here, you would do better to remove those obscure `kcg_` types, which as far as I can tell are just aliases for `char` and `float`. – TonyK Aug 04 '21 at 12:13

1 Answers1

0

Your data types and pointers are all over the place - and you are attempting to return a reference to a temporary local buffer. Suggest the idiomatic:

kcg_char* OP_StringToFloat( const kcg_float d, kcg_char* buffer )
{
    srintf(buffer, "%f", d ) ;
    return buffer;
}

Example call:

kcg_char a[255] ;
kcg_float d = 3.125 ;
OP_StringToFloat( d, a ) ;

You can use the return value in order to use the call as a parameter for example:

printf( "%s\n", OP_StringToFloat( d, a ) ) ;

Note that the sprintf() call is only valid if kcg_float is an alias for float or double.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • Just realized I meant to name the function float to string. Would something like this work? `kcg_char* OP_StringToFloat( const kcg_float32 d ) { kcg_char* a = (char*)malloc(255 * sizeof(char)); snprintf(a, sizeof(a),"%f", d); return a; }` – Justin Young Aug 04 '21 at 18:22
  • @JustinYoung : Post a new question; code does not work well in comments. While it work work, it is a really bad idea. The user has to know that the function has hidden memory allocation and is then responsible for freeing it. If you later change the function to use a static for example, any attempt to free it will cause undefined behaviour. Also `sizeof(char) == 1` by definition - it is pointless, and here in any case you would do better to use `sizeof(*a)`, because 1) a is not a `char`, and 2) that way you can change the type of `a` without introducing an error. – Clifford Aug 04 '21 at 18:30
  • @JustinYoung ... but its is academic in any case because you really don't want to do that - its just nasty. – Clifford Aug 04 '21 at 18:30