Please advise which coding style approach is better: Variant A or Variant B?
Major question is whether to use arrays or structure in example function below.
Thank you for advice.
Common definitions:
Code:
typedef struct MsgHeader
{
int MagicNumber;
int OpCode;
int DataSize;
int Checksum;
int Err;
}
MsgHeader;
typedef struct
{
MsgHeader hdr;
int data[10];
}
CommBlock;
typedef struct
{
int nMinorVersion ;
int nMajorVersion ;
int nBuildNumber ;
}
CmdGetVersion;
Variant A:
Code:
int GetVersion(CommBlock* pCommand)
{
int enErr = eVMD_NOERROR ;
// Check op code
if(pCommand->hdr.OpCode != eVMD_GET_VERSION )
{
enErr = eVMD_BAD_OPCODE ;
}
pCommand->hdr.DataSize = 3 ;
pCommand->data[VMD_CMD_GET_VERSION_0] = VMD_MINOR_VERSION ; // Minor VMD software version
pCommand->data[VMD_CMD_GET_VERSION_1] = VMD_MAJOR_VERSION ; // Major VMD software version
pCommand->data[VMD_CMD_GET_VERSION_2] = VMD_BUILD_NUMBER ; // VMD build number
return enErr ;
}
Variant B:
Code:
int GetVersion(CommBlock* pCommand )
{
int enErr = eVMD_NOERROR ;
CmdGetVersion objReply ;
memset(&objReply, 0, sizeof(CmdGetVersion ) ;
// Check op code
if(pCommand->hdr.OpCode != eVMD_GET_VERSION )
{
enErr = eVMD_BAD_OPCODE ;
}
pCommand->hdr.DataSize = 3 ;
objReply.nMinorVersion = VMD_MINOR_VERSION ; // Minor VMD software version
objReply.nMajorVersion = VMD_MAJOR_VERSION ; // Major VMD software version
objReply.nBuildNumber = VMD_BUILD_NUMBER ; // VMD build number
memcpy(pCommand->data, &objReply, sizeof(CmdGetVersion ) ;
return enErr ;
}