需要將數字和字符值的組合存盤在陣列中
我需要將以下陣列存盤在 atmega328p EEPROM 中,但它不起作用。錯誤狀態為“多字符字符常量”。錯誤在這個陣列中, unsigned char col5[30]= {'17','12A','74','23','30','21','31','10','15', '33','1','14','11','34','3','9','9A','11A','4A','2','16','5 ','4AX','13','75','4','7','6','35A','8','\0'};
是否有可能正確的方法將上述值存盤在陣列中?
#include <avr/io.h>
#include <avr/eeprom.h>
// macro for easier usage
#define read_eeprom_array(address,value_p,length) eeprom_read_block ((void *)value_p, (const void *)address, length)
#define write_eeprom_array(address,value_p,length) eeprom_write_block ((const void *)value_p, (void *)address, length)
//declare an eeprom array
float EEMEM SWGA[68];
float EEMEM MA[68];
float EEMEM SWGT[41];
float EEMEM TPCS[41];
float EEMEM Core_area[30];
//char EEMEM *EI_LAM[31];
// declare a ram array and initialize
float col1[28]={47,46,45,44,43,42,41,40,39,38,37,
36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20};
float col2[28]={8,10,13,16,19,24,30,36,39,50,72,90,110,132,151,182,
221,240,290,344,422,508,628,760,940,1210,1600,2030};
float col4[28] = {27546,20223,14392,11457,9337,7755,6543,5595,4838,3507,2800,
2286,1902,1608,1308,1137,997,881,711,609,
504,415,341,286,242,176,137,106};
float col6[30]= {1.213,1.897,2.284,2.723,3.000,3.329,3.703,4.439,4.839,5.880,6.555,6.555,7.259,7.259,7.562,7.865,7.865,9.072,10.284,10.891,10.891,12.704,13.039,14.117,15.324,15.865,18.969,19.356,39.316,40.803};
unsigned char col5[30]= {'17','12A','74','23','30','21','31','10','15','33','1',
'14','11','34','3','9','9A','11A','4A','2','16','5',
'4AX','13','75','4','7','6','35A','8','\0'};
//unsigned char col5[30]= {'017','12A','074','023','030','021','031','010','015','033','001','014','011','034','003','009','09A','11A','04A','002','016','005','4AX','013','075','004','007','006','35A','008','\0'};
// declare another ram array
//float my_other_ram_array[68];
int main(void)
{
// Copy data from my_ram_array to eeprom array
write_eeprom_array(SWGA,col1,sizeof(SWGA));
write_eeprom_array(MA,col2,sizeof(MA));
write_eeprom_array(TPCS,col4,sizeof(TPCS));
write_eeprom_array(Core_area,col6,sizeof(Core_area));
//write_eeprom_array(EI_LAM,col5,sizeof(EI_LAM));
}
uj5u.com熱心網友回復:
一種方法是使用字串陣列:
char col5[30][4] = {
"17", "12A", "74", "23", "30", "21", "31", "10", "15", "33",
"1", "14", "11", "34", "3", "9", "9A", "11A", "4A", "2", "16",
"5", "4AX", "13", "75", "4", "7", "6", "35A", "8"
};
C 中的字串只是一個以零結尾的字符陣列。由于您的值都不超過 3 個字符,因此長度為 4 的陣列字串將包含每個加終止空位元組。
uj5u.com熱心網友回復:
您在字符和字串之間存在混淆。char 是一種基本資料型別,大小為 1 個位元組,其中字串是一個以空字符結尾的字符陣列
你試圖寫 '17' 這是非法的,因為 char 資料型別應該有一個雙引號中的單個字符
對于您的情況,您應該選擇二維字符陣列或一維字符指標陣列
char array[][4] = {
"17", "12A", "74", "23", "30", "21", "31", "10", "15", "33",
"1", "14", "11", "34", "3", "9", "9A", "11A", "4A", "2", "16",
"5", "4AX", "13", "75", "4", "7", "6", "35A", "8"
}; 確保列的大小必須比最大字串長度大 1,否則會溢位
char* array[]={
"17", "12A", "74", "23", "30", "21", "31", "10", "15", "33",
"1", "14", "11", "34", "3", "9", "9A", "11A", "4A", "2", "16",
"5", "4AX", "13", "75", "4", "7", "6", "35A", "8"
};
你最好使用指標陣串列示法
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/473143.html
標籤:C
