0

我有一个数组,int* array有超过 10.000 个 int 值,但我想指向每个 100 个位置,这意味着我将有int ** matrix,其中:
matrix[i][j],我想i从我的矩阵指向array[i * 100],你如何替换地址?这是我所做的:

u_int8_t **matrix = (u_int8_t **)malloc(width * sizeof(u_int8_t *));

int width_cr = 0;
for (int i = 0; i < width; i ++) {
    if (i % 100 == 0) {

        u_int8_t *position = matrix[width_cr];
        position = &array[i];
        width_cr ++;           

    }
}

问题是它指向数组的开头

4

1 回答 1

2

存储array[i]in的地址matrix[i / 100]

#define HOW_MUCH_NUMBERS 10000

[...]
{
  int array[HOW_MUCH_NUMBERS];
  int i = 0;
  int **matrix;

  matrix = malloc(sizeof(*matrix) * (HOW_MUCH_NUMBERS / 100));
  while (i < HOW_MUCH_NUMBERS)
  {
    matrix[i / 100] = &array[i];
    i += 100;
  }
  [...]
}
于 2012-09-13T12:53:51.640 回答