JavaEar 专注于收集分享传播有价值的技术资料

2个回答

    最佳答案

  1. 英文原文

    float obstaculos[20][4];
    

    … is an array of arrays of float values. Due to sizeof requirements it's a contiguous sequence of float values in memory.

    float**
    

    … is a pointer to a pointer to float. The single sequence of float values (the obstaculos matrix) can naturally convert to a direct pointer (namely to the first item), but there is no pointer stored in that memory that you can get a pointer to pointer to.

    So, one possibility is to do

    using Row = float[4];
    Row obstaculos[20];
    
    Row* getObstaculos() {
        return obstaculos;
    }
    

    But it would be better to define a Matrix, e.g.

    #include <array>
    
    using Row = std::array<float, 4>;
    using Matrix = std::array<Row, 20>;
    
    Matrix obstaculos;
    
    Matrix* getObstaculos() {
        return &obstaculos;
    }
    

    If you really need an array of pointers, then you might want to make the matrix originally of that form.


    Amendment: Here's one way to compute an array of pointers to the rows, if you need that:

    using Row_data_pointers = std::array<float*, std::tuple_size<Matrix>::value>;
    
    auto data_pointers( Matrix& m )
        -> Row_data_pointers
    {
        Row_data_pointers result;
        for( int i = 0; i < int( std::tuple_size<Row_data_pointers>::value ); ++i )
        {
            result[i] = &m[i][0];
        }
        return result;
    }
    

    中文翻译

      float obstaculos [20] [4];
     

    &hellip;是 float 值的数组数组。由于 sizeof 要求,它是内存中 float 值的连续序列。

     浮**
     

    &hellip;是指向 float 的指针的指针。 float 值的单个序列( obstaculos 矩阵)可以自然地转换为直接指针(即第一个项目),但是没有指针存储在该内存中你可以得到指向指针的指针。

    所以,一种可能性就是

     使用Row = float [4];
    Row obstaculos [20];
    
    Row * getObstaculos(){
        返回障碍;
    }
     

    但最好定义一个矩阵,例如

      #include&lt; array&gt;
    
    使用Row = std :: array&lt; float,4&gt ;;
    使用Matrix = std :: array&lt; Row,20&gt ;;
    
    矩阵障碍;
    
    Matrix * getObstaculos(){
        回归与障碍;
    }
     

    如果你真的需要一个指针数组,那么你可能想要制作最初该表格的矩阵。


    修订: 如果需要,这是计算行指针数组的一种方法:

     使用Row_data_pointers = std :: array&lt; float *,std :: tuple_size&lt; Matrix&gt; :: value&gt ;;
    
    auto data_pointers(Matrix&amp; m)
         - &GT; Row_data_pointers
    {
        Row_data_pointers结果;
        for(int i = 0; i&lt; int(std :: tuple_size&lt; Row_data_pointers&gt; :: value); ++ i)
        {
            结果[i] =&amp; m [i] [0];
        }
        返回结果;
    }
     
    float obstaculos[20][4];
    

    … is an array of arrays of float values. Due to sizeof requirements it's a contiguous sequence of float values in memory.

    float**
    

    … is a pointer to a pointer to float. The single sequence of float values (the obstaculos matrix) can naturally convert to a direct pointer (namely to the first item), but there is no pointer stored in that memory that you can get a pointer to pointer to.

    So, one possibility is to do

    using Row = float[4];
    Row obstaculos[20];
    
    Row* getObstaculos() {
        return obstaculos;
    }
    

    But it would be better to define a Matrix, e.g.

    #include <array>
    
    using Row = std::array<float, 4>;
    using Matrix = std::array<Row, 20>;
    
    Matrix obstaculos;
    
    Matrix* getObstaculos() {
        return &obstaculos;
    }
    

    If you really need an array of pointers, then you might want to make the matrix originally of that form.


    Amendment: Here's one way to compute an array of pointers to the rows, if you need that:

    using Row_data_pointers = std::array<float*, std::tuple_size<Matrix>::value>;
    
    auto data_pointers( Matrix& m )
        -> Row_data_pointers
    {
        Row_data_pointers result;
        for( int i = 0; i < int( std::tuple_size<Row_data_pointers>::value ); ++i )
        {
            result[i] = &m[i][0];
        }
        return result;
    }
    
      float obstaculos [20] [4];
     

    &hellip;是 float 值的数组数组。由于 sizeof 要求,它是内存中 float 值的连续序列。

     浮**
     

    &hellip;是指向 float 的指针的指针。 float 值的单个序列( obstaculos 矩阵)可以自然地转换为直接指针(即第一个项目),但是没有指针存储在该内存中你可以得到指向指针的指针。

    所以,一种可能性就是

     使用Row = float [4];
    Row obstaculos [20];
    
    Row * getObstaculos(){
        返回障碍;
    }
     

    但最好定义一个矩阵,例如

      #include&lt; array&gt;
    
    使用Row = std :: array&lt; float,4&gt ;;
    使用Matrix = std :: array&lt; Row,20&gt ;;
    
    矩阵障碍;
    
    Matrix * getObstaculos(){
        回归与障碍;
    }
     

    如果你真的需要一个指针数组,那么你可能想要制作最初该表格的矩阵。


    修订: 如果需要,这是计算行指针数组的一种方法:

     使用Row_data_pointers = std :: array&lt; float *,std :: tuple_size&lt; Matrix&gt; :: value&gt ;;
    
    auto data_pointers(Matrix&amp; m)
         - &GT; Row_data_pointers
    {
        Row_data_pointers结果;
        for(int i = 0; i&lt; int(std :: tuple_size&lt; Row_data_pointers&gt; :: value); ++ i)
        {
            结果[i] =&amp; m [i] [0];
        }
        返回结果;
    }
     
  2. 参考答案2
  3. While float* and float[N] are similar types (they can be used in a similar way, and the first can be implicitly be initialized from the second), but this doesn't hold for nested arrays.

    Why is this so?

    The type float** is a pointer to pointer. When you use it like a 2D array / matrix (with two subscript operators being applied), the first pointer is expected to point to an array of pointers which point to more arrays (e.g. to individual rows).

    However, float[N][M] is a contiguous block of memory for the whole matrix. There is no array of (row) pointers, and none can be created implicitly by casting to float**, thus this cast isn't possible by the standard.

    What can you do?

    You can return a reference to a multidimensional array. For this, first create a typedef for your matrix: typedef float matrix[20][4]; and then use the return type matrix&

    Or avoid nested arrays. Use a one dimensional array and compute the index when accessing (e.g. matrix[row * N + col] instead of matrix[row][col]). Hide this index computation in a matrix class, which returns a helper object for the first operator[].

    Better: Use a Linear Algebra library for handling matrices, for example Eigen.