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

Need Expert C answer

This is not a problem but a question of you C experts. I am trying to use Keil tools so I thought I would start here.
The following typedef is declared in a header file that is included in a header file that is included in a header file that is included in a header file about five deep.
My question is basic C. Why would the programmer use
a struct of one uint16?
I can understand a callback to define a table of records in non-volatile memory. Don't ask me how the programmer planned to use this code. It's not used anywhere!

typedef struct
{ uint16 numRecs;
} nvBindingHdr_t;

By the way, the code is from Texas Instruments Z-Stack
for their ZigBee CC2530 devices.
Please educate me a bit.
Bradford

Parents
  • One reason to hide a single integer in a struct is that the integer representation may change later, to a more complex form.

    Having a struct, means that functions will take a pointer to the struct as parameter, making the code compatible even if the struct suddenly contains two integers or a string.

    The time stamp you receive from time() is also intended to be different depending on environment - you call time(time_t *t) even if time_t is an integer. But since everyone "knows" that time_t is an integer, people still write code that will break if time_t changes. Had time_t been defined to always be a struct, it would have been easier to let even an 8-bit program have a 48-bit or 64-bit range even if the language doesn't have a data type of that size.

    But as already noted - it's almost impossible to second-guess the decisions behind some larger body of code. Especially from that tiny fragment. Another thing - larger programs often have a bit of cruft. You may find a number of variables, data types or functions that are no longer used in a meaningful way.

Reply
  • One reason to hide a single integer in a struct is that the integer representation may change later, to a more complex form.

    Having a struct, means that functions will take a pointer to the struct as parameter, making the code compatible even if the struct suddenly contains two integers or a string.

    The time stamp you receive from time() is also intended to be different depending on environment - you call time(time_t *t) even if time_t is an integer. But since everyone "knows" that time_t is an integer, people still write code that will break if time_t changes. Had time_t been defined to always be a struct, it would have been easier to let even an 8-bit program have a 48-bit or 64-bit range even if the language doesn't have a data type of that size.

    But as already noted - it's almost impossible to second-guess the decisions behind some larger body of code. Especially from that tiny fragment. Another thing - larger programs often have a bit of cruft. You may find a number of variables, data types or functions that are no longer used in a meaningful way.

Children