我有以下代码,以便能够访问结构数组中的众多字段(为简单起见,我将其减少为两个)。最终指针计算的正确咒语是什么,
*(ptr + offset) = data;
因为我总是得到:
错误:从类型 'int32_t' {aka 'int'} 分配给类型 'struct osc_in_data' 时类型不兼容
#define NumHarmonics 10
int32_t data1;
int32_t data2;
struct osc_in_data
{
int32_t LevelAttackRate;
int64_t LevelPeakLevel;
int32_t LevelDecayRate;
} OscControl[NumHarmonics];
void SetADSRvalues(int32_t offset, int32_t data)
{
int32_t harmonic;
struct osc_in_data *ptr;
for (harmonic = 0; harmonic < NumHarmonics; harmonic++)
{
ptr = &OscControl[harmonic];
*(ptr + offset) = data;
}
}
SetADSRvalues(offsetof(struct osc_in_data, LevelAttackRate), data1)
SetADSRvalues(offsetof(struct osc_in_data, LevelDecayRate), data2)