Taypac
Taypac

Reputation: 23

Pass specific structure to function in C

I have been writing a program in C to draw characters on a display using a PIC.

I have a number of structures containing the symbols and metadata for different fonts and I am struggling to work out how to pass them to the function that draws the symbol in the display buffer. The code I have currently will not compile.

So far I have structures that follow this format in a header file:

struct Arial
{
    char symbol[1000] = {...};
    int info[32][3] = {...};
};

struct Courier
{
    char symbol[1000] = {...};
    int info[32][3] = {...};
};

struct Calibri
{
    char symbol[1000] = {...};
    int info[32][3] = {...};
};

Ideally in the rest of my code I would like to pass a reference to one of the structures in the arguments of the display function so it knows which font to use. I am having trouble getting my head around how to do this and I'm getting confused.

For example if I wanted to use the font Arial to print the 10th character in the Arial structure:

void display_function(struct *font_name, int letter)
{
    int letter_start = font_name->info[letter][0];
    char letter_data = font_name->symbol[letter_start];

    // Draws to buffer here
}

void main()
{
    display_function(&Arial, 10);
}

Any help on the way forward would be much appreciated. I have tried searching for other similar questions but can't find one that I have been able to work out.

Upvotes: 2

Views: 73

Answers (1)

dbush
dbush

Reputation: 223699

A struct definition does not by itself create a variable. It only defines a type and its members. Because of that, you can't specify initializers in a struct definition.

You need to create variables of that type. Then you can initialize them. In this case you only need one type and three variables of that type.

struct font
{
    char symbol[1000];
    int info[32][3];
};

struct font Arial = { 
    "abcdefg...",
    {
        { 1, 2, 3 },
        { 4, 5, 6 },
        ...
    }
};

struct font Courier = { 
    "qwerty...",
    {
        { 9, 8, 7 },
        { 2, 3, 4 },
        ...
    }
};


void display_function(struct font *font_name, int letter)
{
    int letter_start = font_name->info[letter][0];
    char letter_data = font_name->symbol[letter_start];

    // Draws to buffer here
}

void main()
{
    display_function(&Arial, 10);
}

Upvotes: 5

Related Questions