blah blah blah is here! blah blah » Close

up0down
link

Hello
I wrote the following code, and got an error:

unsafe void Get_ADC_Samples_From_All_Enabled_Channels(TextBox tb_OutputNumber_ADC, TextBox tb_Voltage_ADC)
{

bool got;

double[,] channelData = new double[64,8];
double*[] voltagePointerArray = new double*[64];

for (int i = 0; i != 64; i++)
{
voltagePointerArray[i] = &(channelData[i, 0]);
got = digitalIODll.getADCSamplesFromAllEnabledChannels( 8, &voltagePointerArray[i] );
if (got == true)
{
tb_Voltage_ADC.Text = "1";
}
else
{
tb_Voltage_ADC.Text = "2";
}
}


}

Error 2 You can only take the address of an unfixed expression inside of a fixed statement initializer


I do not understand what this means, can anyone please verify this for me.

Thanks
Muhu

last answered 2 years ago

1 answers

up0down
link

As channelData and voltagePointerArray are managed arrays, you need to 'pin' them (using the 'fixed' statement) before you can take addresses of their elements:

unsafe void Get_ADC_Samples_From_All_Enabled_Channels(TextBox tb_OutputNumber_ADC, TextBox tb_Voltage_ADC)
{
bool got;

double[,] channelData = new double[64,8];
double*[] voltagePointerArray = new double*[64];

fixed(double* pCD = channelData)
{
fixed(double** pVPA = voltagePointerArray)
{
for (int i = 0; i != 64; i++)
{
pVPA[i] = &pCD[i * 8];
got = digitalIODll.getADCSamplesFromAllEnabledChannels(8, &pVPA[i]);

if (got)
{
tb_Voltage_ADC.Text += "1"; // probably += rather than =
}
else
{
tb_Voltage_ADC.Text += "2"; // ditto
}
}
}
}
}

Feedback