Skip to main content
Associate II
December 2, 2023
Solved

How to make a variable float to const float

  • December 2, 2023
  • 12 replies
  • 7204 views

Dear All,

I compile a program with cube ide and would like to know how the float variable is const float.

sample is. 

float a

const float  b

b = a;

BR,

Methee

    This topic has been closed for replies.
    Best answer by Tesla DeLorean

    And I can't edit or pull code from a screen shot/bitmap.

    Make a structure you're configuration items live in.

    Create routines to read, write and validate this structure you can place in FLASH or EEPROM

    FOO foo = { 0 };
    
    void InitFoo(FOO *foo, EEPROM *eeprom)
    {
    ...
     foo->donkeys = 12;
     foo->freqHz = eeprom->freqHz;
     foo->speedRPM = eeprom->speedRPM; 
    ...
    }

     

    12 replies

    TDK
    December 2, 2023

    Const (constant) variables cannot be changed after they are initialized. This means you can and must initialize them only once.

    It's a compiler flag more than anything that lets the compiler know that variable won't be changing and it can optimize things as if that variable will always be the same value.

    "If you feel a post has answered your question, please click ""Accept as Solution""."
    methee_cAuthor
    Associate II
    December 2, 2023

    Dear TDK,

    You can help me for this solution?

    float a =10;

    const float  b=a;

    BR, 

    Methee

     

    TDK
    December 2, 2023

    That code is valid and will compile just fine. What help is needed?

    "If you feel a post has answered your question, please click ""Accept as Solution""."
    Tesla DeLorean
    Guru
    December 2, 2023

    To what end?

    const float b = 10.0f; 

    Or pass it to a function that has const float x as a parameter?

    The optimizer should try to fold constant values, perhaps use define?

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    methee_cAuthor
    Associate II
    December 3, 2023

    Dear Tesla DeLorean,

     

    I need to read data from eeprom for change information in parameters of MotorControl Workbench 6.2.0 project.

    But function parameters need to be const float or #define only.

    Please help advise.

    const float.jpg

     

    BR,

    Methee

    Piranha
    Principal III
    December 3, 2023

    That is not a function, but a structure initialization. You need to learn the C programming language first before trying to deal with complex software development.