This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

question on structures

Hi all,

I'm coding graphic LCD routines and I want to include several text fonts.

So, I've defined a structure as :

typedef struct DEF_FONT
{
	U8 u8width;     /* Character width*/
	U8 u8height;  /* Character height*/
	U8 *au8fontTable;       /* Font table start address in memory  */
} DEF_FONT;

then, I've included two fonts and they are declared like this :
struct DEF_FONT code Font_System5x8 = {5, 8, au8font5x8};
struct DEF_FONT code Font_System8x8 = {8, 8, au8font8x8};

Now, I want to print a text with a selected font.

I've tried to send the structure as a parameter in the GLCD_PutChar function as :
void LcdPutchar (U8 u8Char, DEF_FONT stFont)

and then, I've tried to access the elements of the structure in the function like this :

LcdDataWrite (stFont->au8fontTable[(u8Char - 32) * 5 + u8CharColumn]);

There is an error from compiler
***ÿERROR 199 IN LINE 345 OF C:\TRX8051\GLCD.C: left side of '->' requires struct/union pointer

I suppose the solution may be to declare in the function a pointer to the structure like this:
struct DEF_FONT *ptr
I understand how to use it in a single structure, but in this case, how is done the selection of the font between the two structures ?



Regards
Stephane

Parents Reply Children
  • You can pass structures:

    void myfunc (DEF_FONT font);

    or you can pass pointers to structures.

    void myfund (DEF_FONT* font);

    They're not the same thing, any more than passing integers versus pointers to integers. People usually pass pointers to structures to save stack space and time.

    If you pass the structure, then you need to use the '.' operator to dereference fields:

    font.au8fontTable;

    If you pass a pointer to the structure, you need to use the '->' operator:

    font->au8fontTable;

    The Keil bug references says that the compiler will incorrectly allow you to use '.' to dereference a pointer.

    However, you shouldn't (and shouldn't be able to) use the "arrow" operator -> on an actual structure.



  • I've well understood the difference between the '.' and '->' operators now.

    The suggestion from Dan Henry is working well. My code is working correctly.
    I had forgotten to define the function using the pointer.....


    Thanks everybody

    Regards
    Stephane