only for C++ experts: how to place string to PROGMEM w/o array

Is it possible to place string to progmem w/o using an array ?

I tried const PROGMEM char *const ptr = "NUMBER1";
I tried const PROGMEM char *const ptr PROGMEM = "NUMBER1";
I tried const PROGMEM char *const ptr PROGMEM = (PGM_P)"NUMBER1";

but no luck , pointer in flash, string in sram. should be some c++ tricks. may be reinterpret cast, may be something else, but
I Do Not Believe That This Is Impossible :slight_smile:

PS: I know how to store in PROGMEM an array of chars

What's wrong with defining it as an array? A c-string is after all an array....

const char str[] PROGMEM = "Hello world!!!!!!!!!!!!!";

Although

const char* const str PROGMEM = "Hello world!!!!!!!!!!!!!";

seems to result in the same.

Have a look at this page:

Putting constant data into program memory

About half-way down the page is a method that creates a structure composed of a character array, then puts the strings into progmem using the structure, but that requires that all strings be the same length.

The other method is to create the strings as character arrays in progmem, then create an array of pointers to the character arrays in progmem.

If you only have a single string, instead of an array of strings, it should be a bit easier.

septillion, nothing wrong with an array , just want to know if it possible w/o array . but this const char* const one PROGMEM = "NUMBER1"; place string to sram.

david_2018, I know how to put array to progmem , question about how to put string w/o array. Nick uses array to store string

const PROGMEM char const ptr = "NUMBER1";
is a shortcut for
const char spaceinmemory[] = "NUMBER1";
const char
ptr PROGMEM = spaceinmemory;

so spaceinmemory will not be handled as PROGMEM

Yes , my string formally, looks like in your example. and question how to put and string also to progmem , but with one line :slight_smile: w/o declare an array in PROGMEM

may be
const char *const ptr PROGMEM = (some magic attribute here)"NUMBER1";
but for this need to know c++ as an expert

@alexblade: You're trying to solve a problem that doesn't exist. A "string" (aka c-string) IS AN ARRAY (with a null termination at the end). Just use the array notation and get on with things!!!

this is not "invented problem", simple I not well know c++. if language doesnt allow to us in the same line mark/declare the string as progmem-string then , yes I will use an array , as additional row of code to declare string in progmem

Why do you want to waste two bytes on a superfluous pointer?

I understand that will be strlen_P + 2. but this question not about speed/capacity/optimization. I didnt say that need/better to use pointer instead array. I just want figure out about possibility c++

In regular manuals no info about progmem :slight_smile:

alexblade:
this is not "invented problem", simple I not well know c++. if language doesnt allow to us in the same line mark/declare the string as progmem-string then , yes I will use an array , as additional row of code to declare string in progmem

according to C++11 specification, C++ doesn't have syntax to mark a string literal with 'attributes'.
only functions and variables can have 'attributes' .

PROGMEM is attribute((progmem))

the progmem attribute is used by AVR specific linker script to move the variable to dedicated section of the final hex or binary. the build tools don't understand the attribute, only pass it over

How are you going to use the string? If it is for a print statement, the F () function will do the work of putting it into progmem for you.

david_2018, no , thank you, for print I have my own func and send to it PSTR() w/o helping F() , Juraj helped me to understand this theme

This should put the string into progmem

const PROGMEM char str[] = "NUMBER1";

It just cannot be accessed directly, will need to be copied to ram first, for most instructions.

Delta_G, sure string is array of chars, my concern was not about how sequence of chars is in the memory

(for me hard to explain correct) but I mean something like on post#5

const char *const ptr PROGMEM = (some magic attribute/trick/conversation here)"NUMBER1";

david_2018:
It just cannot be accessed directly,

Im not expert, but Im using direct access w/o copy to memory

I understand and agree. Sure, group of characters is an array. OK. could we make this (two rows of code)

const PROGMEM char str[] = "NUMBER1";
const PROGMEM char* ptr = str;

somehow in one row ?

Why do you need the extra pointer variable (ptr)? The name 'str' will act as a pointer when needed.

just to know what can c++

Im not "getting" into expert. too hard for me :slight_smile: sorry.
yes I thought may be exist some alternative, but I understand - no. sorry and thank you for your time