1

好吧,我是新来的,所以请放轻松:)

我目前正在使用 C 语言开发 uC,并在每次用户想要创建结构时使用链表来创建结构。例子:

typedef struct {
char*                   dataitem;
struct listelement      *link;
int16_t                 wordSize;
int16_t                 (*libWord)[Q];
char                    gpioValue;
}listelement;

listelement * AddItem (listelement * listpointer, char* name, int16_t size, int16_t wordLength, int16_t (*words)[L][Q]) {
// returns listPointer at the beginning of list
listelement * lp = listpointer;
listelement * listPointerTemp;
char ErrorHandler = NULL;
// are we at the end of the list?
if (listpointer != NULL) {
    // move down to the end of the list
    while (listpointer -> link != NULL)
    listpointer = listpointer -> link;
    listPointerTemp = listpointer;
    listpointer -> link = (struct listelement  *) malloc (sizeof (listelement));
    // on fail end links becomes NULL already above
    if(listpointer -> link != NULL){
        listpointer = listpointer -> link;
        listpointer -> link = NULL;
        listpointer -> wordSize = wordLength;

        listpointer -> dataitem = (char*) malloc ((size + 1)*sizeof(char));
        if(listpointer -> dataitem != NULL){
            for(int i=0; i<size ; i++){
                listpointer -> dataitem[i] = name[i];
            }
            listpointer -> dataitem[size] = NULL;

            listpointer -> libWord =  (int16_t(*)[Q])malloc(wordLength*Q*sizeof(int16_t));
            if(listpointer -> libWord != NULL){
                for (int16_t row=0 ; row < wordLength ; row++){
                    for (int col=0 ; col < Q ; col++){
                        listpointer -> libWord[row][col]  = words[0][row][col];
                    }
                }
                ErrorHandler = 1;
            }else{
                free(listpointer->dataitem);
                free(listpointer);
                listPointerTemp -> link = NULL;
            }
        }else{
            free(listpointer);
            listPointerTemp -> link = NULL;
        }
    }
    if(ErrorHandler == NULL){
        //failure
        usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
        usart_write_line(&AVR32_USART0,"Ran out of Memory!  Word not created.\r\n");
        usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
    }
    return lp;
}
else {
    listpointer = (struct listelement  *) malloc (sizeof (listelement));

    if(listpointer != NULL){
        listpointer -> link = NULL;
        listpointer -> wordSize = wordLength;

        listpointer -> dataitem = (char*) malloc (sizeof(name));
        if(listpointer -> dataitem != NULL){
            for(int16_t i=0; i<size ; i++){
                listpointer -> dataitem[i] = name[i];
            }

            listpointer -> libWord =  (int16_t(*)[Q])malloc(wordLength*Q*sizeof(int16_t));
            if(listpointer -> libWord != NULL){
                for (int16_t row=0 ; row < wordLength ; row++){
                    for (int col=0 ; col < Q ; col++){
                        listpointer -> libWord[row][col]  = words[0][row][col];
                    }
                }
                ErrorHandler = 1;
            }else{
                free(listpointer->dataitem);
                free(listpointer);
                listPointerTemp -> link = NULL;
            }
        }else{
            free(listpointer);
            listPointerTemp -> link = NULL;
        }
    }
    if(ErrorHandler == NULL){
        //failure
        usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
        usart_write_line(&AVR32_USART0,"Ran out of Memory!  Word not created.\r\n");
        usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
    }
    return listpointer;
}
}

所以现在我想使用 const 添加已经存储在内存中的结构:

    // start to include words, then merge into structures for user_interactive to link lists with
//////////////////////////////////////////////////////////////////////////////////////////////////
//                                          TRON
//////////////////////////////////////////////////////////////////////////////////////////////////

const int16_t libWord1[10][Q] = {   {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},        
                            {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
                            {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
                            {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
                            {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
                            {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
                            {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
                            {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
                            {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
                            {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},    };


const char dataitem1[4] = {'T','r','o','n'};

const int16_t wordSize1 = 10, nameSize = 4;

//////////////////////////////////////////////////////////////////////////////////////////////////

// eventually change NULL to next link in link list
const listelement Tron = { dataitem1, NULL, wordSize1, libWord1, 0x00 }; 

好吧,即使你跳过所有这些,我只是想知道有什么好的方法可以以某种方式创建常量结构(我必须为 const 类型创建一个新的结构格式吗?)然后链接 RAM 链表的开头当用户在启动时创建的链表的 Flash 部分创建第一个?

如果需要更多信息,请告诉我。我尽量不要在这里放太多代码。

谢谢!

所以这里是变化和效果:

// start to include words, then merge into structures for user_interactive to link lists with
//////////////////////////////////////////////////////////////////////////////////////////////////
//                                          TRON
//////////////////////////////////////////////////////////////////////////////////////////////////

const int16_t libWord1[10][Q] = {   {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},        
                                    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
                                    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
                                    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
                                    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
                                    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
                                    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
                                    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
                                    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
                                    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},    };

//////////////////////////////////////////////////////////////////////////////////////////////////
//                                          ON
//////////////////////////////////////////////////////////////////////////////////////////////////

const int16_t libWord2[10][Q] = {   {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},        
                                    {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
                                    {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
                                    {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
                                    {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
                                    {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
                                    {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
                                    {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
                                    {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
                                    {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},    };

//////////////////////////////////////////////////////////////////////////////////////////////////

// eventually change NULL to next link in link list
const listelement* Tron = { "Tron", NULL, 10, libWord1, 0x00 };
// next element links to Tron . . .
const listelement* On = { "On", Tron, 10, libWord2, 0x01 };
// next element links to On . . . and so on . . .

listelement* constLinkInit(void){
    return On;
}

我知道我没有做你的全局结构数组,但忽略了它并使用函数将最后一个结构传递给另一个文件中的这个人。

listelement *listpointer;

    // initially listpointer is initialized to NULL but now set to end of link list of FLASH
    listpointer = constLinkInit();

即使传递给 listpointer (On) 的地址是正确的,当指向下一个链接时,像我对用户定义的结构所做的那样动态地打印链接(它已经工作)失败了。

void PrintQueue (listelement * listpointer) {
    usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
    if (listpointer == NULL){
        usart_write_line(&AVR32_USART0,"queue is empty!\r\n");
    }else{
        //usart_write_line(&AVR32_USART0,"               Word List                    \r\n");
        //usart_write_line(&AVR32_USART0,"--------------------------------------------\r\n");
        while (listpointer != NULL) {
            //usart_write_line(&AVR32_USART0, listpointer -> dataitem);
            //usart_write_line(&AVR32_USART0,"\r\n");
            //writeCOM_int_array(listpointer -> libWord , listpointer -> wordSize);
            listpointer = listpointer -> link;   // <---- FAILS HERE
        }
    }
    usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
}

当指向下一个链接时,它会转到错误的地址。. .

谢谢您的帮助!

4

1 回答 1

0

像这样的东西应该工作:

#include <stdlib.h>
#include <stdint.h>

#define Q 5


typedef struct {
 char                    *dataitem;
 struct listelement      *link;
 int16_t                 wordSize;
 int16_t                 (*libWord)[Q];
 char                    gpioValue;
 } listelement;

int16_t zzz[Q] = { 11, 22,33,44,55};

listelement global_array[] =
{{ "Sron", global_array+1, 2, zzz , 0 }
,{ "Tron", global_array+2, 2, zzz , 0 }
,{ "Uron", global_array+3, 2, zzz , 0 }
,{ "Vron", NULL          , 2, zzz , 0 }
};
于 2013-05-11T16:14:38.977 回答