首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PROGMEM (Arduino)中创建一个链接列表

在PROGMEM (Arduino)中创建一个链接列表
EN

Stack Overflow用户
提问于 2013-11-28 03:54:18
回答 1查看 1.9K关注 0票数 1

我正在研究一个问题,我认为这个问题基本上可以通过一个(多个)链表很好地解决。但是,我的平台是一个具有非常有限的SRAM的Arduino,因此我希望将其全部保存在PROGMEM中(使用avr/pgmspace.h库)。

引用具有指针的结构的字段时有问题。或者,换句话说,我在跟踪我的链接列表时遇到了困难。

下面是一些代码(我试图缩短代码):

代码语言:javascript
复制
#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()
{
}

它的产出如下:

代码语言:javascript
复制
--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函数来读取结构,但我不明白为什么它似乎适用于第一项。谢谢你的帮助或指点,我应该读一些东西来理解这一点。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-02 01:30:51

好了,解决了。在PROGMEM中获取struct字段中的指针的正确方法是:

代码语言:javascript
复制
the_next_item = (list_item*) pgm_read_byte(&(the_next_item->next_item));

我确实很困惑。而且,它似乎适用于前几个例子的事实是一条让我走错了路的红鲱鱼。我还是不能完全解释-可能只是运气?我没有把握。

有关指针和PROGMEM的最有用的资源,请参见此处:http://www.nongnu.org/avr-libc/user-manual/pgmspace.html

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20257394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档