confusion about flash-strings

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

Hello,

 

I am a little bit confused at the moment regarding flash-strings on UC3.

Has anyone an idea why this doesn't compile?

typedef struct {
	uint8_t cp;
	uint8_t font;
	uint8_t align;
	const void *str;
}flash_string_meta_t;

     [...]

const char str_en_0[] = "apple";
const char str_en_1[] = "banana";
const char str_en_2[] = "peach";

const char* test_strings_en[] = {
	str_en_0,
	str_en_1,
	str_en_2
};

const flash_string_meta_t test_strings_meta_en[] = {
	{ .cp = 1,	.font = 0,	.align = 0,	.str = test_strings_en[0] },
	{ .cp = 1,	.font = 0,	.align = 0,	.str = test_strings_en[1] },
	{ .cp = 1,	.font = 0,	.align = 0,	.str = test_strings_en[2] }
};

     [...]

glcd_print_string((char *)test_strings_meta_en[0].str, 0, 55, &font5x8, 0, 0);

     Compiler output:

Error    171    initializer element is not constant    ...

     (which refers to test_strings_meta_de[0])

 

The equivalent approach for 8-bit AVRs is like this:

http://www.atmel.com/webdoc/AVRLibcReferenceManual/FAQ_1faq_rom_array.html

 

 

This topic has a solution.
Last Edited: Wed. Dec 2, 2015 - 01:32 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I assume you mean it refers to test_strings_meta_en[0]?  There's no definition for a ..._de[0].

 

Anyway, "test_strings_en" is not a constant according to the C standard.  It doesn't matter that you have your variable defined as const, it's still a variable as far as initialization is concerned.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

this works for me now :)

(note the pointer-pointer on "str")

typedef struct {
	uint8_t cp;
	uint8_t font;
	uint8_t fmt;
	void **str;
}flash_string_meta_t;

const flash_string_meta_t test_strings_meta_en[] = {
	{ .cp = 0,	.font = 0,	.fmt = 0,	.str = &((char *){"voltage"}) },
	{ .cp = 0,	.font = 0,	.fmt = 0,	.str = &((char *){"current"}) },
	{ .cp = 0,	.font = 0,	.fmt = 0,	.str = &((char *){"temperature"}) }
};

glcd_print_string((char *)*test_strings_meta_en[0].str, 0, 55, &font5x8, 0, 0);

which gives me the possibility to do this:

 

const flash_string_meta_t test_strings_meta_cn[] = {
	{ .cp = 1,	.font = 1,	.fmt = 0,	.str = &((wchar_t *){L"电压"}) },
	{ .cp = 1,	.font = 1,	.fmt = 0,	.str = &((wchar_t *){L"当前"}) },
	{ .cp = 1,	.font = 1,	.fmt = 0,	.str = &((wchar_t *){L"温度"}) }
};

glcd_print_wstring((wchar_t *)*test_strings_meta_cn[0].str, 0, 55, &font16_prop, 0, 0);

 

Last Edited: Wed. Dec 2, 2015 - 10:57 AM