View previous topic :: View next topic |
Author |
Message |
csanders
Joined: 23 Apr 2005 Posts: 16
|
Returning a structure from a function |
Posted: Mon Oct 24, 2005 10:06 pm |
|
|
Hello Everyone
I'm sure this has been posted somewhere before, but after hours of searching I can't find it, so I'll just ask in a new topic.
I am wanting to return a structure from a function, but the code below is has a problem I can't correct.
The compile error I receive is 'Expecting a (' with the fuction name highligted.
Does any one out there see where my problem is?
Code: |
typedef struct RegValue {
byte OnesDigit:4;
byte TensDigit:4;
byte Value;
};
RegValue read_rtc(byte address)
{
struct RegValue work;
byte i;
i2c_start();
i2c_write(0x70);
i2c_write(address | write);
i2c_stop();
i2c_start();
i2c_write(0x73);
i = i2c_read(0);
i2c_stop();
work.onesdigit = i & 0x0F;
work.tensdigit = i & 0xF0;
work.value = (work.tensdigit * 10) + work.onesdigit;
return (work);
} |
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Oct 25, 2005 6:40 am |
|
|
First problem is the typedef
Code: | typedef struct _RegValue {
byte OnesDigit:4;
byte TensDigit:4;
byte Value;
}RegValue;
|
Next problem is
Code: | struct RegValue work; |
should be
since you planned on using typedef
Next problem is with
Code: | pWork->tensdigit = i & 0xF0; |
I believe that you meant
Code: | pWork->tensdigit = (i>>4) & 0x0F; |
I think its is probably better to pass a pointer to the function than to return a struct
Code: |
void read_rtc(byte address, RegValue pWork)
{
byte i;
i2c_start();
i2c_write(0x70);
i2c_write(address | write);
i2c_stop();
i2c_start();
i2c_write(0x73);
i = i2c_read(0);
i2c_stop();
pWork->onesdigit = i & 0x0F;
pWork->tensdigit = (i>>4) & 0x0F;
pWork->value = (work->tensdigit * 10) + work->onesdigit;
} |
It may be more efficient to store the ones and tens in separate variable and do the value calculation on those vars.
Code: |
void read_rtc(byte address, RegValue pWork)
{
byte ones;
byte tens;
i2c_start();
i2c_write(0x70);
i2c_write(address | write);
i2c_stop();
i2c_start();
i2c_write(0x73);
ones = i2c_read(0);
i2c_stop();
tens = (ones>>4) & 0x0F;
ones &= 0x0F;
pWork->onesdigit = ones;
pWork->tensdigit = tens;
pWork->value = (tens* 10) + ones;
} |
RJ or PCM can probably comment on wether the pointer is better or if passing a struct is better. I guess you can compile the two and see which is more efficient. I like to use the pointer method so that I can return true or false depending on wether or not the function succeeds. Say for example the I2C failed to read a value. Then I could return false to indicate that. |
|
|
Ttelmah Guest
|
|
Posted: Tue Oct 25, 2005 7:40 am |
|
|
I suspect I'd probably pass the pointer, since otherwise the whole structure has to be transferred back. One alternative would be just to return a pointer to the structure, and have it declared as static. A lot would depend I think on how often the function is going to be called, and whether you want the storage 'saved' by allowing it to be re-used. Passing it back will probably be the slowest, but will make the most efficient 'use' of the space, allowing all the areas used inside the routine to be used by other code.
Best Wishes |
|
|
|