Need to store a combination of numeric and character values in an array
I need to store the below array in atmega328p EEPROM but it isn't working.The error states "multi-character character constant". Error is in this array, 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'};
Is there a possible correct way to store above values in an array?
#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));
}
CodePudding user response:
One approach is to use an array of strings:
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"
};
A string in C is just a zero-terminated array of characters. Since none of your values is longer than 3 characters, an array strings of length 4 will hold each one plus terminating null bytes.
CodePudding user response:
you are having a confusion between a char and a string. The char is a fundamental datatype and of size 1 byte where as the string is a null terminated character array
you are trying to write '17' which is illegal because char data type should have a single character in double quotes
for your case you should either go for a 2 dimensional character array or an one dimensional array of character pointers
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"
}; make sure that the size of column must be 1 greater than the largest string length other wise there will be overflow
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"
};
you better go for the pointer array notation