c - 如何创建这个公式?

标签 c pointers matrix memory-address

我接受过培训/作业来创建应用于 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/

相关文章:

c++ - IPv6 到 IPv4 的转换只给出 0.0.0.1

c - 如何用表格制作 "global variable"?

c++ - 在指针中使用 **

c++ - 将指向 reference_wrapper 的指针转换为指向底层的指针?

python - 如何在 C 中编写一个函数,它将两个矩阵作为 numpy 数组的输入

c - 如何创建新行之前的隐形空间?

c - 对于函数样式或类型构造,应为 '('

c++ - 删除有两个 child 的目标

python - 如何在Python中以某种方式调整矩阵的大小?

Javascript:更新矩阵数组