我接受过培训/作业来创建应用于 C 的以下公式:
How to calculate the address to an element in a matrix of integers, if the starting address of the matrix is known, the size is known and the row and the columns of the element is known.
假设矩阵是 a[b][c]。
起始地址是 a
或等效的 &a[0][0]
行数是b。
列数是c。
每个元素的大小都应该是sizeof(int)
,这样地址就是a + b*sizeof(int) + a*sizeof(int)
这个公式正确吗?
最佳答案
假设你有int array[b][c]
,你需要找到元素array[i][j]
的地址。
a + i * c * sizeof(int) + j * sizeof(int)
其中,c * sizeof(int)
看起来是一个行大小,所以一般公式是
array_start + i * row_size + j * item_size
关于c - 如何创建这个公式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12818273/