我有一个头文件,声明这些数组:
int stVal_On[] = {2};
int stVal_Off[] ={1};
int subVal_On[]={1};
int subMss[]={1};
int subVal_Off[]={0};
Run Code Online (Sandbox Code Playgroud)
然后在声明的结构中使用解除引用的数组:
WriteData结构的定义:
/* Write structure used in loop for Read- and Write Tests */
typedef struct WriteData {
char* name; // MMS object name
const VOID* data; // Data to write
const SINT32 localFormat; // SVI type (on server)
const SINT32 dataLength; // length of data to write/read
const SINT32 NbrofElmnts; // Number of elements to write/read
char* description; // SVI type as String (on server)
char* SVI_Name; // SVI address of the SVI mapped on server
UINT32 svi_Length; // length of SVI variable on server (used for readback)
} WriteData;
Run Code Online (Sandbox Code Playgroud)
这个int arr[] = {1};成语的目的是什么?如果只分配了一个值,为什么要使用数组呢?
好吧,我能想到的一个原因与代码组织有关.如果您以表格形式编写代码:
struct {
char const *file_name;
uint16_t flags;
// Other data
} table [] = {
{ .file_name = "/usr/bin/foo", .flags = 0x0 },
};
for (size_t i = 0; i < sizeof(table)/sizeof(table[0]); ++i) {
// do something meaningful with a table row table[i]
}
Run Code Online (Sandbox Code Playgroud)
虽然它现在只是1的数组,如果你需要添加更多的情况,你的代码已经很好地编写了.您只需要在表初始化程序中添加另一个"行".