char数组在函数中无法识别

Che*_*mbe 1 c arrays embedded string-literals

我正在为一个班级做一个项目.嵌入式C代码.我正在尝试创建一个由全局声明的5个字符串的字符数组,因此我的LCD函数可以轻松地遍历列表.它们可以被声明为const但是现在我只想让它构建没有问题.

问题是我在函数中遇到"未声明"错误,并且在构建时指向声明的"冲突类型"错误.声明看起来正确,但我想不是.我错过了什么?一旦声明被整理出来,未声明的错误可能会自行修复.

     // Declared before main()
     char _cylinder_types[5];

    _cylinder_types[0] = "Blk";
    _cylinder_types[1] = "Wht";
    _cylinder_types[2] = "Stl";
    _cylinder_types[3] = "Alu";
    _cylinder_types[4] = "Err";
Run Code Online (Sandbox Code Playgroud)

在我的lcd.c文件中:

void lcd_display_update(void){

  int i = 0;
  while(i<5)
    {
     lcd_write(0);
     lcd_position(lcd_TopLine,1);
     lcd_string("SORTED:");
     lcd_string(_cylinder_types[i]);
     lcd_write(':');
     lcd_write_Num_8(drop_number[i]);

     lcd_position(lcd_BotLine,1);
     lcd_string("UNSORTED:");
     lcd_string(_cylinder_types[i]);
     lcd_write(':');
     lcd_write_Num_8(queued_number[i]);

     mTimer(5000);
    }
     i++;
}
Run Code Online (Sandbox Code Playgroud)

Vla*_*cow 6

只需声明数组即可

char * _cylinder_types[5];
^^^^^^ 
Run Code Online (Sandbox Code Playgroud)

例如,在此表达式语句中

_cylinder_types[0] = "Blk";
Run Code Online (Sandbox Code Playgroud)

字符串文字"Blk"隐式转换为该类型的右值char *.

你可能不会发表这些陈述

_cylinder_types[0] = "Blk";
_cylinder_types[1] = "Wht";
_cylinder_types[2] = "Stl";
_cylinder_types[3] = "Alu";
_cylinder_types[4] = "Err";
Run Code Online (Sandbox Code Playgroud)

在任何功能之外.

例如,您可以初始化数组

 char * _cylinder_types[5] =
 {
     "Blk", "Wht", "Stl", "Alu", "Err"
 };
Run Code Online (Sandbox Code Playgroud)

如果项目中有多个编译单元,那么数组应该在标题中声明

extern char * _cylinder_types[5];
Run Code Online (Sandbox Code Playgroud)

并且在某些模块中定义了例如

 char * _cylinder_types[5] =
 {
     "Blk", "Wht", "Stl", "Alu", "Err"
 };
Run Code Online (Sandbox Code Playgroud)

标头必须包含在每个模块中,其中有对数组的引用.

考虑到这个说法

 i++;
Run Code Online (Sandbox Code Playgroud)

应该在while循环中.