Access to PROGMEM Compiler warning

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dear community,

I need some help with the following code. The code works, but I cannot suppress the compiler warning:

 

What ist wrong here?

friendly regards

Ellen

 

/*
 *  testProgmem.c
 *
 *  Author : User2
 */
#include <avr/io.h>
#include <avr/pgmspace.h>

volatile char c;
const char str[] PROGMEM = "This is a constant string.\r"
			   "This is too big to place it into RAM\r"
			   "Therefore I must place it into Prog Area\r";

void doSomething(char * arr)
{
	while (pgm_read_byte(&(*arr)))
	  c = (pgm_read_byte(&(*arr++)));
}

int main(void)
{
    doSomething(str);
    while (1)
    {
    }
}

 

This topic has a solution.
Last Edited: Wed. Apr 17, 2019 - 11:24 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What it's saying is that "str" is of type "const char []" (aka "const char *") but your interface to do_something() is:

void doSomething(char * arr)

So that is just expecting "char *" (or "char []"). So you are passing something that is "const" into a function that is not going to treat it as const. So it is warning you "doSomething discards 'const' qualifier". There are two possible remedies:

int main(void)
{
    doSomething((char *)str);

This is a bit "dangerous". You are saying "yeah, yeah, I know that str is really const char * but for now let's just pretend it's only const char". This is one of the reasons that MISRA and various static code analysers will suggest you don't use typecasts (also the reason that C++ now has 3 types of typecast when C only has one). The safer solution is to carry the const all the way through:

void doSomething(const char * arr)
{

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PS this is 2019. Unless this is C++ use __flash not PROGMEM

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

I can not understand why I did not think so myself Type casting.

The first solution seems to be dangerous. You may can write into die Prog Mem.

This will receive unexspected results.

Both solutionns are working now.

Many thanks.#