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
  • a question of you C experts.

    Actually, no, it's not. It's a question about the data structure design of an (apparently somewhat involved) piece of software. The fact that it's expressed in C syntax is quite irrelevant.

    Why would the programmer use a struct of one uint16?

    That's strictly impossible to tell from such a microscopic fragment. You're basically asking for a mind reading of a person neither you or us has likely ever met.

    As-is, the only conclusion to be drawn from this fragment is that somebody might have over-done the abstraction or data-hiding a bit.

Reply
  • a question of you C experts.

    Actually, no, it's not. It's a question about the data structure design of an (apparently somewhat involved) piece of software. The fact that it's expressed in C syntax is quite irrelevant.

    Why would the programmer use a struct of one uint16?

    That's strictly impossible to tell from such a microscopic fragment. You're basically asking for a mind reading of a person neither you or us has likely ever met.

    As-is, the only conclusion to be drawn from this fragment is that somebody might have over-done the abstraction or data-hiding a bit.

Children
  • 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.

  • Gents;
    Thank you both for your feedback.
    Per;
    What you write makes a great deal of sense to me.
    Thanks again,
    Bradford