This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Question on constants

Hallo,
i have not understand the difference of locating constants in the following example:
Txt1 is located in Rom, while Txt2 (declared inside a function) is a variable in Ram and will be copied in ram.

const char Txt1[] = "Hallo1";

void main(void)
{
const char Txt2[] = "Hallo2";
char c1, c2;
	c1 = Txt1[0];
	c2 = Txt2[0];

	while(1)
		;
}

Can you explain me the difference?

thanks
Eugen

Parents
  • This is a basic C question (that's why I removed the toolset specification).

    The difference between the two is what C calls "storage duration" of a variable. Your Txt2 is of "automatic" storage duration, Txt1 is of "static" storage duration. It's pretty much guaranteed to be an error to have a single variable both "const" and "automatic". Change the declaration of Txt2 to

    static const char Txt2[]="Hallo2";

Reply
  • This is a basic C question (that's why I removed the toolset specification).

    The difference between the two is what C calls "storage duration" of a variable. Your Txt2 is of "automatic" storage duration, Txt1 is of "static" storage duration. It's pretty much guaranteed to be an error to have a single variable both "const" and "automatic". Change the declaration of Txt2 to

    static const char Txt2[]="Hallo2";

Children