Chef Flambe
Chef Flambe

Reputation: 883

char array not recognized in function

I'm working on a project for a class. Embedded C-code. I'm attempting to create a char array of 5 strings that I declare globally so my LCD function can loop through the list easily. They could be declared as const but right now I just want it to build without issue.

Problem is I keep getting an 'undeclared' error in the function and 'conflicting types' error pointing at the declaration when I build. The declaration looks correct but I guess it's not. What am I missing? The undeclared error will probably fix itself once the declaration is sorted out.

     // 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";

inside my lcd.c file:

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++;
}

Upvotes: 1

Views: 412

Answers (2)

user3629249
user3629249

Reputation: 16540

the following proposed code:

  1. does not yet compile because it is missing, amongst other things, the include statements for the header files.
  2. demonstrates how to initialize the array when it is declared, rather than via a series of assignment statements.
  3. properly declares the array as an array of pointers to strings
  4. uses an indent width of 4 spaces as that is very readable
  5. separates code blocks (for, if, else, while, do...while, switch, case, default) via a single blank line
  6. does not name the array, starting with _ as such names are 'reserved' of the OS
  7. allows the compiler to determine the size of the array
  8. uses the 'preprocessor' operator 'sizeof' to calculate the second parameter of the for() statement.
  9. breaks the for() statement so it does not runoff the right edge of the page
  10. uses appropriate horizontal spacing, inside parens, after semicolons, after commas, etc for readability
  11. replaces the incorrect lcd_write( 0 ); (which should be passed a character, not a integer) with lcd_write( '0' );

and now, the proposed code:

char *cylinder_types[] =
{
    "Blk",
    "Wht",
    "Stl",
    "Alu",
    "Err"
};


int main( void )
{
    for(int i=0;
        i < sizeof( cylinder_types ) / sizeof( char * );
        i++ )
    {
        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 );
    }
}

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 311048

Just declare the array like

char * _cylinder_types[5];
^^^^^^ 

For example in this expression statement

_cylinder_types[0] = "Blk";

the string literal "Blk" is implicitly converted to an rvalue of the type char *.

And you may not place these statements

_cylinder_types[0] = "Blk";
_cylinder_types[1] = "Wht";
_cylinder_types[2] = "Stl";
_cylinder_types[3] = "Alu";
_cylinder_types[4] = "Err";

outside any function.

You could for example initially initialize the array like

 char * _cylinder_types[5] =
 {
     "Blk", "Wht", "Stl", "Alu", "Err"
 };

If there are several compilation units in the project then the array should be declared in the header like

extern char * _cylinder_types[5];

and in some module defined like for example

 char * _cylinder_types[5] =
 {
     "Blk", "Wht", "Stl", "Alu", "Err"
 };

The header must be included in each module where there is a reference to the array.

Take into account that this statement

 i++;

shall be inside the while loop.

Upvotes: 6

Related Questions