2

I have a file like this:

#include <avr/io.h>
#include <avr/pgmspace.h>

const PROGMEM char* str = "Hello UART!\r\n";

I'm trying to compile it with a Makefile, this is the final command:

avr-gcc -std=gnu99 -mmcu=atmega328p -DF_CPU=16000000UL -I. -Ilib/
    -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
    -Wall -Wno-main -Wno-strict-prototypes -Wno-comment -g2 -Wextra
    -Wfatal-errors -Wno-unused-but-set-variable -ffunction-sections
    -fdata-sections -Wl,--gc-sections -Wl,--relax -Os main.c lib/uart.c
    --output main.elf

I am getting the following error:

main.c:9:21: error: variable 'str' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
 const PROGMEM char* str = "Hello UART!\r\n";
                     ^
compilation terminated due to -Wfatal-errors.
Makefile:71: recipe for target 'main.elf' failed
make: *** [main.elf] Error 1

What's wrong with my code?

I tried moving the PROGMEM keyword in various places of the declaration, without any change.

1
  • Try: const char str[] PROGMEM = "Hello UART!\r\n"; Commented Apr 26, 2015 at 6:38

2 Answers 2

4
const char str[] PROGMEM = "Hello UART!\r\n";

should work. You're creating a non-const pointer to const data.

Sign up to request clarification or add additional context in comments.

4 Comments

will this put the \0 at the end, too?
Yes it will 0 terminate the string.
I still don't really understand the difference, but this works. Maybe you could try to add some more info? I always thought array is the same as pointer in c.
const char str[] PROGMEM = "Hello UART!\r\n"; declares a const array of 14 bytes (including 0 termination) to be put in program memory. const PROGMEM char* str = "Hello UART!\r\n"; only declares a non-const pointer to be put in program memory pointing to const data not in program memory. The non-const pointer cannot be put in program memory and therefore gives you the error.
0

Your code declares a non-const pointer to a(n array of) const char. That is, the compiler is exactly right that variable str is non-const, it is the thing to which it points that is declared const. If you want both pointer and pointed-to array to be const, then that would be this:

const PROGMEM char* const str = "Hello UART!\r\n";

or equivalently, this:

PROGMEM char const * const str = "Hello UART!\r\n";

Those two mean exactly the same thing as each other.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.