如何使用数字和字符值用 C 语言编写数组
How to code an array in C language with numeric and charactor values
需要在数组中存储数字和字符值的组合
我需要将以下数组存储在 atmega328p EEPROM 中,但它不是 working.The 错误状态“多字符字符常量”。
错误在这个数组中,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','[=10=]'};
//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','[=10=]'};
// 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));
}
一种方法是使用字符串数组:
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 中的字符串只是一个 zero-terminated 字符数组。由于 none 个值超过 3 个字符,长度为 4 的数组字符串将包含每个值加上终止空字节。
您混淆了字符和字符串。字符是
基本数据类型,大小为 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"
};
你最好使用指针数组表示法
需要在数组中存储数字和字符值的组合
我需要将以下数组存储在 atmega328p EEPROM 中,但它不是 working.The 错误状态“多字符字符常量”。 错误在这个数组中,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','[=10=]'};
//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','[=10=]'};
// 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));
}
一种方法是使用字符串数组:
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 中的字符串只是一个 zero-terminated 字符数组。由于 none 个值超过 3 个字符,长度为 4 的数组字符串将包含每个值加上终止空字节。
您混淆了字符和字符串。字符是 基本数据类型,大小为 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"
};
你最好使用指针数组表示法