我正在研究一个问题,我认为基本上可以通过一个(乘法)链表很好地解决这个问题。但是,我的平台是具有非常有限的SRAM的Arduino,因此我希望将其全部保存在PROGMEM中(使用avr / pgmspace.h库)。
我在引用具有指针的结构的字段时遇到麻烦。或者,换句话说,我在跟踪链接列表时遇到了麻烦。
这是一些代码(我已经试过简短了):
#include <avr/pgmspace.h>
typedef struct list_item
{
const prog_char * header;
const struct list_item *next_item;
};
// declarations
extern const list_item PROGMEM first_item;
extern const list_item PROGMEM second_item;
extern const list_item PROGMEM third_item;
// name
const prog_char first_header[] PROGMEM = "Foo";
const prog_char second_header[] PROGMEM = "Bar";
const prog_char third_header[] PROGMEM = "Baz";
// instantiation & initialization
const list_item first_item = { &first_header[0], &second_item };
const list_item second_item = { &second_header[0], &third_item };
const list_item third_item = { &second_header[0], &first_item };
// pointers to our items, just for testing
list_item const * const pointer_to_first_item = &first_item;
list_item const * const pointer_to_second_item = &second_item;
list_item const * const pointer_to_third_item = &third_item;
// prints the address of the pointer passed to it
void print_pointer_address( char * description, const void * pointer)
{
Serial.print(description);
Serial.println((unsigned int) pointer,HEX);
}
// a test
void setup()
{
Serial.begin(57600);
Serial.println("\n--addresses of everything--");
print_pointer_address("pointer to first_item = ", pointer_to_first_item);
print_pointer_address("pointer to second_item = ", pointer_to_second_item);
print_pointer_address("pointer to third_item = ", pointer_to_third_item);
Serial.println("\n--go through list via pointers--");
list_item const * the_next_item;
the_next_item = pointer_to_first_item;
print_pointer_address("item 1 = ", the_next_item);
the_next_item = the_next_item->next_item;
print_pointer_address("item 2 = ", the_next_item);
the_next_item = the_next_item->next_item;
print_pointer_address("item 3 = ", the_next_item);
the_next_item = the_next_item->next_item;
print_pointer_address("item 4 = ", the_next_item);
}
void loop()
{
}
它输出:
--addresses of everything--
pointer to first_item = 68
pointer to second_item = 6C
pointer to third_item = 70
--go through list via pointers--
item 1 = 68
item 2 = 6C
item 3 = 1
item 4 = 5350
我的问题是:为什么第3项不等于“70”?
我认为可能应该使用pgmspace函数之一来读取结构,但随后我不明白为什么它似乎适用于第一项。感谢您的帮助或对我应该读懂的东西的指导。
最佳答案
好,解决了在PROGMEM的struct字段中获取指针的正确方法是:
the_next_item = (list_item*) pgm_read_byte(&(the_next_item->next_item));
我当然很困惑。同样,它似乎在前几个示例中都有效,这是一条红鲱鱼,这使我误入歧途。我仍然无法完全解释-可能只是运气吗?我不确定。
有关指针和PROGMEM的最有用资源,请参见此处:http://www.nongnu.org/avr-libc/user-manual/pgmspace.html