(btw I'm not allowed to malloc in this, I'm writing in c for c99) I'm trying to create a wrapper struct in c for arrays to keep things tidy so I don't need to keep passing the array pointer and the length around, and can just use a struct:
#define MAX_LEN 64
typedef struct {
uint8_t data[MAX_LEN];
size_t len;
} byteArray_t;
Which is fine if MAX_LEN is known, but is there a way to make the length variable although known at compile time, so that for example I could have something like:
typedef struct {
byteArray_t tag;
byteArray_t length;
byteArray_t value;
} tlv_t;
In which the array corresponding to tag would have size MAX_TAG_LEN and so on for the others - so I'd need to tell the compiler somehow that that was the case...
Anyone got any ideas? Thanks!
Edit
Alright, sorry for the confusion. Here's what I'm trying to do. I basically have the following structures at present:
// tag object
typedef struct {
uint8_t data[MAX_TAG_LENGTH_IN_BYTES];
uint32_t len;
} tlvTag_t;
// length object
typedef struct {
uint8_t data[MAX_LENGTH_OF_LENGTH_IN_BYTES];
uint32_t len;
} tlvLength_t;
typedef struct tlv tlv_t;
// value object definition
typedef struct {
// can consist of a byte array, or a list of sub TLVs
union {
uint8_t data[MAX_VALUE_LENGTH_IN_BYTES];
// data can be parsed into subTLVs
tlv_t* subTLVs;
};
// need to store the number of subTLVs
uint32_t numberOfSubTLVs;
// len is the total length of the data represented:
// the total length of the subTLVs placed end to end
// or the length of the data array.
uint32_t len;
} tlvValue_t;
// tlv object definition
struct tlv {
tlvTag_t tag;
tlvLength_t len;
tlvValue_t value;
// total length of entire tlv block (not value length)
// (if there are sub TLVs, place them end to end)
uint32_t totalLen;
};
I thought the design would be better if I could wrap the arrays in another struct to avoid all the code duplication and be able to pass fewer arguments around, but I can't because I don't know how to tell the compiler to create different sized byte arrays - maybe it's possible using macros? Hope that makes sense.
/Dfor MSVC for example).