This function is given to me by an API:
void GetTime(uint8_t*, uint8_t*, uint8_t*, uint8_t*, uint8_t*, uint8_t* );
GetTime returns the time by writing to those pointers. Problem is, I only need the value written to one of those pointers. If I could control GetTime I'd either check if the pointers were NULL or return a struct instead, but I can't.
This is my current solution, ignoring the dummy* pointers:
uint8_t *seconds, *dummy0, *dummy1, *dummy3, *dummy4, *dummy5;
GetTime( dummy0, dummy1, dummy3, dummy4, dummy5, seconds );
Is there a neater way to solve this problem with less clutter?
First, you are doing it wrong, you need to allocate a uint8_t and pass a pointer to it.
Secondly you can use the same dummy for all:
uint8_t seconds, dummy;
GetTime( &dummy,&dummy,&dummy,&dummy,&dummy, &seconds );
Finally, the API may accept NULL. Does this work?:
uint8_t seconds;
GetTime( NULL,NULL,NULL,NULL,NULL, &seconds );
As suggested before, you could have a function like this that helps you call GetTime
uint8_t dummy[5];
uint8_t seconds;
GetTime(dummy, dummy + 1, dummy + 2, dummy + 3, dummy + 4, &seconds);
return seconds;
If you know that GetTime works properly with pointers that aren't unique, you could just get away with this (but then again, you're only saving 4 bytes):
uint8_t dummy, seconds;
GetTime(&dummy, &dummy, &dummy, &dummy, &dummy, &seconds);
return seconds;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With