Skip to main content
Graduate
April 27, 2024
Question

Initialized const pointer

  • April 27, 2024
  • 2 replies
  • 1211 views

How do I correct this code so that the pointer is a constant and not place into RAM?

```

const typeOfVariable Variable = DEFINED_VARIABLE_VALUE;
const typeOfVariable *pVariable = &Variable;

```

`pVariable` keeps ending up in RAM and I've run out of ideas :-(.

Thanks!

    This topic has been closed for replies.

    2 replies

    Graduate II
    April 27, 2024

    Not familiar with the STM8 compiler, but static const ?

    What is it that your ultimately trying to achieve? Unfortunately the static will limit it to one source file, but also preclude the linker or other sources changing it. The static const is workable to force tables/arrays in to the text rather than data sections in most ARM / x86 tools I've used.

    The code pasting tool is via the </> icon in the edit box, the triple quote method doesn't work here.

    Graduate
    April 28, 2024

    In this particular case I can live with losing two bytes of RAM but I am interested in case this indicates a more general COSMIC/stm8s "feature" that I need to be aware of.  I copied this code from another processor where this construct works and am puzzled as to why it doesn't with the COSMIC toolset as the pointer really won't every change and everything the linker needs to resolve this is in hand.

    Visitor II
    April 28, 2024

    COSMIC compiler did what you told him to do. The pointer should be const and not *pointer. Try this:

    typeOfVariable * const pVariable = &Variable;