I’m using an official SDK that provides an api.h header file. When I try to compile, I get the following error, with wriggles on cmd:
type containing an unknown-size array is not allowed
Here are the relevant typedefs from the SDK:
typedef struct {
/** Command ID. One of CMD_* in fpc_cmds.h. */
uint16_t cmd_id;
/** Type of frame. One of FPC_BP_FRAME_TYPE_*. */
uint16_t type;
/** Placeholder for payload, if any. See fpc_cmds_*.h for command payloads. */
uint8_t payload[];
} fpc_cmd_hdr_t;
typedef struct {
/** Command header. */
fpc_cmd_hdr_t cmd;
/** 96 bits of unique ID. */
uint32_t mcu_unique_id[3];
/** FW ID. */
uint8_t fw_id;
/** Fuse Level. */
uint8_t fw_fuse_level;
/** Version String Length */
uint16_t version_str_len;
/** Version String (incl termination '\0') */
char version_str[];
} fpc_cmd_version_response_t;
It seems the error comes from the member cmd in the structure type fpc_cmd_version_response_t. I am verifying the code with IntelliSense.
Is this a compiler limitation? I don’t think it’s an SDK error since it’s an official commercial SDK. How can I fix or work around this issue?
The structure types you show make use of "flexible array members", a feature of standard C since C99. That's ok for fpc_cmd_hdr_t, and the FAM of fpc_cmd_version_response_t is ok, but because fpc_cmd_hdr_t contains a FAM, it is not ok for fpc_cmd_version_response_t to have a member of type fpc_cmd_hdr_t.
The specifications around this have not changed since the feature's introduction. The current version of the spec puts them this way:
A structure or union shall not contain a member with incomplete or function type [...], except that the last member of a structure with more than one named member can have incomplete array type; such a structure [...] shall not be a member of a structure or an element of an array.
(C24 6.7.3.2/3; emphasis added)
Is this a compiler limitation?
Technically, yes, it is a compiler limitation that it fails to accept that code, but it is an entirely reasonable one. The code violates a language constraint, so not only does the language not define the behavior of a program that includes that code, but a conforming compiler must emit a diagnostic about it. No compiler is obligated to reject it, but a compiler that provides defined behavior for it thereby implements an extension.
I don’t think it’s an SDK error since it’s an official commercial SDK.
Surprise!
What actually constitutes an "error" is open to some debate, but the code presented violates a formal constraint of the C language. That may be knowing or unknowing, and it may work as intended on one or more C implementations, but the problem here is best attributed to the code, not the compiler.
How can I fix or work around this issue?
Do consult the API documentation. It may have something relevant to say.
You may be able to find a different compiler that accepts it. The API may provide something to address the situation, such as a macro you can define to avoid the issue. Or maybe you should just be using a different header or a different version of the whole API. Modifying the API technically might be a way you could go, but I don't expect that's practical.
Since it's a commercial API, I recommend contacting the vendor for support.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With