[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-libc-dev] Problem accessing bytes in program space
From: |
Paulo Marques |
Subject: |
Re: [avr-libc-dev] Problem accessing bytes in program space |
Date: |
Mon, 28 Jan 2008 20:37:36 +0000 |
User-agent: |
Thunderbird 1.5.0.12 (X11/20070509) |
Rich Teer wrote:
Hi all,
Hi,
I have some strings that I'm storing in program space like this:
const char PROGMEM sms_keys_0[] = "abcABC0 ";
const char PROGMEM sms_keys_1[] = "-_1 ";
[...]
PGM_P sms_keys[] PROGMEM = {
sms_keys_0,
sms_keys_1,
[...]
};
I want to access characters of each string one at a time. Something like
uart_puts (strcpy_P (buf, (PGM_P)pgm_read_word (&sms_keys[num])))
will correctly print the numth string, but trying to print the string starting
from elsewhere leads to data being read from God knows where:
uart_puts (strcpy_P (buf, (PGM_P)pgm_read_word (&sms_keys[num][2])))
This is not what you want to do. What you want is more like (untested):
uart_puts (strcpy_P (buf, (PGM_P)(pgm_read_word(&sms_keys[num]) + 2)));
or more simply (to send just one char):
uart_send_char(pgm_read_byte(pgm_read_word(&sms_keys[num]) + 2));
The casts are left as an exercise for the reader ;)
I'd expect this to print the numth string starting at the 3 character
(it works as expected in non-embedded normal C).
No it doesn't...
Am I doing something
cluelessly wrong, or am I tickling a bug in the compiler?
At best, you're fetching the address of the second character of string
"num", reading a pointer from that address, and then using that pointer
to copy a string from.
--
Paulo Marques
Software Development Department - Grupo PIE, S.A.
Phone: +351 252 290600, Fax: +351 252 290601
Web: www.grupopie.com
"One of my most productive days was throwing away 1000 lines of code."
Ken Thompson