Dear Friends: I never met the following definition before, would you help me to find out how they can define them like this?
#define ResetInfo(info) info.cmd = 0; info.status = MI_OK; info.irqSource = 0; info.nBytesSent = 0; info.nBytesToSend = 0; info.nBytesReceived = 0; info.nBitsReceived = 0; info.collPos = 0; typedef struct { unsigned char cmd; //!< command code char status; // communication status unsigned char nBytesSent; // how many bytes already sent unsigned char nBytesToSend; // how many bytes to send unsigned char nBytesReceived;// how many bytes received unsigned short nBitsReceived; // how many bits received unsigned char irqSource; // which interrupts have occured unsigned char collPos; // at which position occured a // collision } MfCmdInfo; static volatile MfCmdInfo MInfo; ResetInfo(MInfo);
Could you make your question more specific? The code appears to be a typical C typedef of a structure (record), followed by the definition of a variable of that type. There's also the definition of a macro used to initialize the structure. Stylistically, I'd think this somewhat questionable. That's what functions are for. I'd also probably use memset() instead of a whole series of assignments to 0. What is it exactly that you are trying to find out?
Since I never met this kind of definition before, it is wired to me..
#define ResetInfo(info) info.cmd = 0; info.status = MI_OK; info.irqSource = 0; info.nBytesSent = 0; info.nBytesToSend = 0; info.nBytesReceived = 0; info.nBitsReceived = 0; info.collPos = 0;
"I think ResetInfo is a function for resetting MInfo structure, right?" Wrong. It is a Macro - not a function! This is plain, pure vanilla, bog-standard use of #define - see any standard 'C' textbook. As Drew said, it'd probably be easier just to use memset. And, stylistically, a true function would probably be better than a macro.
I'd also probably use memset() instead of a whole series of assignments to 0. Given that there's non-atomic elements in that structure (i.e. anything else than char types), that'd be at least potentially dangerous coding. I'm reasonably sure this doesn't apply to C51, but please keep in mind that in general, memset() is not a valid method of initializing any data structure containing larger-than-byte elements.
"in general, memset() is not a valid method of initializing any data structure containing larger-than-byte elements." Why not? Sure, you may end up initialising some padding bytes, but why is that dangerous?
"Why not?" There is no guarantee that all data types use all-bits-zero to represent zero.
"There is no guarantee that all data types use all-bits-zero to represent zero." Ah yes, that's true. In the specific example posted, I s'pose we don't know that MI_OK is numerically zero. But I don't think that was Hans-Berhard's specific point: "in general, memset() is not a valid method of initializing any data structure containing larger-than-byte elements." (my emphasis). Apart from possibly unnecessary zeroing of padding bytes, what is the actual danger here?
what is the actual danger here? It is theoretically possible that padding bits/bytes have to be something *else* than zero, and that zeroing them yields a signalling representation. I.e. first time you try to use any of those zeroed values, the program could, theoretically, halt. To put it in more accessible terms: all-bits zero for an FP value could conceivably be a NaN.
OK, now I get you!
On the Keil toolchains (C51/C166/C251/CARM) memset with ZERO is a safe way to initialize a struct with 0. Reinhard
"But I don't think that was Hans-Berhard's specific point" Actually, I think that is precisely his point. As far as I understand it, setting padding bytes within a structure to all-bits-zero is safe.