2013-10-14 89 views
0

我在指针世界中有点迷路:-) 使用传递一个数来读取值readSystemVal的工作原理就像是一个魅力,但现在我想写给这些使用writeSystemVal的变量。丢失在pointerworld中,通过数组写入到变量

无法真正找到我应该做的事情: * systemVal将地址保存在变量中,但是如何将其值写入它?

你会好看吗?

(系统:AVR,蚀,atmega644)

// User Changeable variables 
uint8_t  MIDIchan1 = 0;  // midichannel osc 1 
uint8_t  MIDIchan2 = 1;  // midichannel osc 2 
uint8_t  MIDIchan3 = 2;  // midichannel osc 3 
uint8_t  pbRange = 12;  // pitchbend 
uint8_t  omniModus = 0x00; // 3 osc modus or 1 omni modus 
uint8_t  midiThru = 0x01; // midiTrhu on/off 
uint8_t  *systemValList[] = {&MIDIchan1, &MIDIchan2, &MIDIchan3, &pbRange, &omniModus, &midiThru}; 


//************************************************** 
// readSystemVal 
// DESCRIPTION: 
// Hele simpele note off routine 
//************************************************** 
uint8_t readSystemVal (uint8_t systemItem) 
{ 
    uint8_t *systemVal = (uint8_t *)systemValList[(uint8_t)systemItem]; 
    uint8_t returnVal = *systemVal; 

    return returnVal; 
} 

void writeSystemVal (uint8_t ctrlValue, uint8_t systemItem) 
{ 
/* 
    uint8_t *systemVal = (uint8_t *)systemValList[(uint8_t)systemItem]; 
    //uint8_t returnVal = *systemVal; 
    // systemVal = ctrlValue; 
*/ 
} 
+1

你可能在寻找'* systemVal = ctrlValue;'? – Angew

+0

将事物铸造成现有类型只会导致混乱和潜在的未来错误。 – molbdnilo

回答

4

否,systemVal(没有星号)保持的地址。这就是指针,只是一个整数,其值是指针指向的地址。

您可以使用取消引用运算符*来读写指针指向的值。

实施例中,指针指向返回值:

return *systemVal; 

实施例中,设置值的指针指向:

*systemVal = ctrlValue; 

顺便说,该readSystemVal函数可以是缩写为:

uint8_t readSystemVal (uint8_t systemItem) 
{ 
    return *systemValList[systemItem]; 
} 

无需中间的额外步骤。尤其不是类型转换,它将值赋予它们实际声明的类型。

+0

谢谢你清理我的错误!我会放手一搏! – user2371490