好吧所以我是新来的,所以请对我很轻松:)
我目前正在使用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类型创建一个新的结构格式?)然后链接开头的当用户在启动时创建的链接列表的Flash部分创建第一个时,RAM链接列表?
如果需要更多信息,请与我们联系。我试着不要在这里放太多代码。
谢谢!
所以这是变化和效果:
// 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");
}
当指向下一个链接时,它会转到错误的地址。 。
感谢您的帮助!
答案 0 :(得分: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 }
};