有如下代码,请帮忙看下这里柔性数组的定义是否可行,有无风险。目的是为了实现结构体中不定长数组元素的初始化。感谢指导!
typedef struct
{
uint8_t fileNo;
uint8_t attr;
uint8_t status;
} S_FILE;
typedef struct
{
uint32_t length
S_FILE fileInfo[]; // 这里使用柔性数组结合length初始化成变长数组是否可行?
} S_FILE_DATA;
S_FILE_DATA gFileData[MAX_PARTITION_CNT]; // 全局变量
void FileInfoInit(S_FILE *file, uint32_t lenght)
{
for (int i = 0; i < length; i++)
{
file[i].fileNo = i;
file[i].attr = i * 2;
file[i].status = 0;
}
}
int main(void)
{
for (int partitionId = 0; partitionId < MAX_PARTITION_CNT; partitionId ++)
{
FileInfoInit(&gFileData[partitionId].fileInfo[0], 32 + partitionId );
}
return 0;
}