Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
13 years ago

Help with basic C code

I want to be able to set the char variable (to be displayed on the LCD) from within the functions (named fx1 and fx2 here).

I'm getting a 'expected expression before ']' token' error.

I hope someone can help, i know its probably quite a basic thing to do...

#include <stdio.h>
# include "system.h"
# include "altera_up_avalon_character_lcd.h"
# define switches (volatile char*) 0x00101060
char first_row;
void fx1(void)
	{
		 first_row = "fx1";
	}
void fx2(void)
	{
		//first_row = "fx2";
	}
int main()
{
	int i;
	int switch_state = 0;
	while(1)
		{
		lcd();
		int switch_state = 0;
		for(i = 0; i < 8; i++)
		{
			if (*switches & (1 << i))
			{
				switch_state = i;
				break;
			}
		}
			switch (switch_state)
			{
			case 0: fx1(); break;
			case 1:	fx2(); break;
		
			}
		}
return 0;
}
int lcd()
{
alt_up_character_lcd_dev * char_lcd_dev;
 // open the Character LCD port
char_lcd_dev = alt_up_character_lcd_open_dev ("/dev/character_lcd_0"); /*open device port*/
if ( char_lcd_dev == NULL)/*port opened?*/
printf ("Error: could not open character LCD device\n");
else
printf ("Opened character LCD device\n");
/* Initialize the character display */
alt_up_character_lcd_init (char_lcd_dev);
/* Write in the first row */
alt_up_character_lcd_string(char_lcd_dev, first_row);
}

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hello,

    Look at this one

    char first_row;

    I C this is how you define an array of characters, in your example I guess 16 might be ok.

    char first_row;

    Google C char arrays :)
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    First, as mortenk said, your array has to have a size. then to assign a string to it, you will need to use strcpy or strncpy (safer) or memcpy.

    
    char first_row ;
    void fx1(void)
    {
       strcpy (first_row, "fx1"); // you should use strncpy here instead.  I did not use it because I do not know what lib you are using.
    }