Why can't we use double pointer to represent two dimensional arrays?

前端 未结 5 1046
我在风中等你
我在风中等你 2020-11-22 02:23

Why can\'t we use double pointer to represent two dimensional arrays?

arr[2][5] = {\"hello\",\"hai\"};
**ptr = arr;

Here why doesn\'t the d

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 02:57

    Having pointer-to-pointer means that each row (or column, if you prefer to think of it that way) can have a different length from the other rows/columns.

    You can also represent a 2D array by just a pointer to the start element, and an integer that specifies the number of elements per row/column:

    void matrix_set(double *first, size_t row_size, size_t x, size_t y, double value)
    {
      first[y * row_size + x] = value;
    }
    

提交回复
热议问题