Pass a struct to a func, func populates data, func returns struct. How?

Hi all,

I've got a library that I wrote for 128x64 pixel VFD displays. One of the things that I want to do is be able to query the library for a "large" number of variables and use them in the sketch. For example, here's what I want to do (pseudo-code):

static MYDRIVER VFD;

struct {
    uint8_t charWidth;
    uint8_t charHeight;
    // lots more
} fontData;

VFD.init (); // init the VFD module
VFD.setFont (8x16_bold); // set text font

VFD.getInfo (&fontData); // pass address of struct to driver

VFD.print ("The font width is "); // display...
VFD.print (fontData.charWidth); // ...data returned to...
VFD.print (" pixels"); // ...the struct

=============================================================================

(inside the driver):

void MYDRIVER::getInfo (const char *str) //
{
    // copy data to the struct that was passed to it
    str.charWidth = _fontWidth; // "_fontWidth" already exists in the library
    str.charHeight = _fontHeight;
}

I've been beating my head against the wall trying to get this to work and I'm just not seeing it. I've Googled "pass struct to function in c" and found several examples, but none of them seem to apply to what I'm trying to do.

I know what I want to do is simple... I'm just not seeing it.

Any help will be greatly appreciated.

Start by giving the struct a name:

struct fontSpec {
    uint8_t charWidth;
    uint8_t charHeight;
    // lots more
} fontData;

Then you can use a reference:

void MYDRIVER::getInfo (struct fontSpec &spec) {
    // copy data to the struct that was passed to it
    spec.charWidth = _fontWidth; // "_fontWidth" already exists in the library
    spec.charHeight = _fontHeight;
}

or a pointer:

void MYDRIVER::getInfo (struct fontSpec *spec) {
    // copy data to the struct that was passed to it
    spec->charWidth = _fontWidth; // "_fontWidth" already exists in the library
    spec->charHeight = _fontHeight;
}

Go with pass-by-reference. The compiler (almost) eliminates the possibility of passing something invalid like NULL.

johnwasser:
Start by giving the struct a name:

struct fontSpec {

uint8_t charWidth;
   uint8_t charHeight;
   // lots more
} fontData;




Then you can use a reference:



void MYDRIVER::getInfo (struct fontSpec &spec) {
   // copy data to the struct that was passed to it
   spec.charWidth = _fontWidth; // "_fontWidth" already exists in the library
   spec.charHeight = _fontHeight;
}




or a pointer:



void MYDRIVER::getInfo (struct fontSpec *spec) {
   // copy data to the struct that was passed to it
   spec->charWidth = _fontWidth; // "_fontWidth" already exists in the library
   spec->charHeight = _fontHeight;
}

So simple. Thanks!