简体   繁体   中英

Access to 1D stored array representing 2D matrix

I'm creating a 2D square matrix in Excel VBA

Dim Matrix(0 To Nrows, 0 To Ncols) as double

and then I pass it as argument ByRef to a function library,

My_foo(Matrix(0,0))

which calls a C++ code. In C++ code I want to access the matrix to get values and make other operations. On this purpose, I read these values as 1D array, so I created an index [R*Ncols + C], where R and C are the element position in 2D representation

void _stdcall My_foo(double* M, long Nrows, long Ncols){

double Value;
R = 10;
C = 0;

Value =  M[R*Ncols + C];

}

At this point I expect to find the element having position (10,0), but I find the element (0,10). It seems my matrix is stored with an inverted column/rows order.

It would be enough to set an index like [C*Nrows + R], and access it by column. I tried this and it works, but it contradicts many blogs and posts...

Why? Is it normal?

尽管C / C ++使用行优先格式来表示多维数组,但对于其他语言却做出了不同的选择 ,因此Excel VBA使用列优先格式也就不足为奇了,就像在本论坛问题中所记录的那样。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM