下面是一個Win32控制檯應用程序過程,演示了陣列上各種指針的依賴關係。例如,通過取消註釋標記爲'// uncomment ...'的行來更改原始數組(模型)中的值會導致輸出更改。我的問題是如何在C#託管代碼環境中獲取或模仿此行爲(即不使用不安全和指針)?C++到C#:指針和陣列
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
float model[100];
for(int i = 0; i < 100; i++) { model[i] = i; }
// uncomment these to alter the results
//model[5] = 5000;
//model[20] = 20000;
//model[38] = 38000;
static const int componentCount = 5;
float* coefs = model; // coefs points to model[0]
float* mean = coefs + componentCount; // mean points to model[0 + componentCount] == model[5]
float* cov = mean + 3*componentCount; // cov points to model[0 + componentCount + 3*componentCount] == model[20]
int ci = 2;
float* c = cov + 9*ci; // c points to model[0 + componentCount + 3*componentCount + 9*ci] == model[38]
int i = 0;
cout <<"model : "<< model[i] << endl; // 0
cout <<"coefs : "<< coefs[i] << endl; // 0
cout <<"mean : "<< mean[i] << endl; // 5 (or 5000)
cout <<"cov : "<< cov[i] << endl; // 20 (or 20000)
cout <<"ci : "<< ci << endl; // 2
cout <<"c : "<< c[i] << endl; // 38 (or 38000)
cin.get(); }
您的意思是您解引用指針使用數組語法,還是有一個情況下,你想我是> 0(意爲過去的平均什麼?值) – 2010-11-28 19:16:26
順便說一句,你在做什麼叫做「指針別名」。 – chrisaycock 2010-11-28 19:20:51
我可能是0以外的東西,它只是爲了舉例。 – descf 2010-11-28 19:27:10