C ++将动态指针传递给2D数组

时间:2014-11-04 14:36:58

标签: c++ arrays pointers

这是我previously提出的上一个问题的扩展。我可以使用模板提交动态2D数组并访问元素。现在,让我们说我有一个指向这样一个数组的指针。我目前正在使用显示here的方法将指针指定给原始的2d数组。我最初认为我可以将模板函数的预期输入更改为(*A)[M][N],但这是不行的。我是否遗漏了一些有关某人可以解释的指针的概念?

header.h

#pragma once

#include <cstddef>
#include <iostream>
using namespace std;

template<size_t N, size_t M>
       void printPtr( int(*A)[M][N]) {
           for(int i=0; i < M; i++){
               for(int j=0; j<N; j++) {
                   cout << A[i][j] << " ";
               }
               cout << endl;
           }
       }

的main.cpp

#include "header.h"
#include <iostream>
using namespace std;

int main() {

    int A[][3] = {
        {1,2,3},
        {4,5,6},
        {7,8,9},
        {10,11,12}
    };

    int (*ptrA)[3] = A;
    printPtr(ptrA);
}

3 个答案:

答案 0 :(得分:1)

如果您不想知道为什么您的代码首先没有工作,请跳到本文末尾

您的代码存在的问题是您已经 模拟&#34;通过指针传递array decaying

int A[][2] = {
    {1,2,3},
    {4,5,6},
    {7,8,9},
    {10,11,12}
};

int (*ptrA)[3] = A;

以下使用C++11 features的代码确认了这一点:

#include <iostream>
#include <type_traits>
using namespace std;

template <typename T, typename U>
struct decay_equiv : 
    std::is_same<typename std::decay<T>::type, U>::type 
{};

int main() {

    int A[][4] = {
            { 1, 2, 3 },
            { 4, 5, 6 },
            { 7, 8, 9 },
            { 10, 11, 12 }
    };

    std::cout << std::boolalpha
              << decay_equiv<decltype(A), int(*)[3]>::value << '\n'; // Prints "true"
}

Example

您应该通过指针或引用将非衰减类型(即具有关于数组维度的所有信息的类型)传递给函数:

#include <cstddef>
#include <iostream>
using namespace std;

template<size_t N, size_t M>
void printPtr(int (*A)[M][N] /* Also a reference could work */) {
   for(int i=0; i < M; i++){
      for(int j=0; j<N; j++) {
         cout << (*A)[i][j] << " ";
      }
      cout << endl;
   }
}

int main() {

    int A[][6] = {
            { 1, 2, 3 },
            { 4, 5, 6 },
            { 7, 8, 9 },
            { 10, 11, 12 }
    };

    int(*ptrA)[4][7] = &A; // Not a decayed type
    printPtr(ptrA);
}

Example

另一种解决方案是首先不使用指向数组的指针,或者在向数组传递引用时取消引用它:

template<size_t N, size_t M>
void printPtr( int(&A)[M][N]) {
     for(int i=0; i < M; i++){
         for(int j=0; j<N; j++) {
             cout << A[i][j] << " ";
         }
         cout << endl;
     }
}

...
printPtr(A);
printPtr(*ptrA);

Example

答案 1 :(得分:1)

您将函数参数定义为指向二维数组的指针

int(*A)[M][N]) 

同时尝试调用函数作为参数传递一个指向一维数组的指针

int (*ptrA)[3] = A;
printPtr(ptrA);

此外,功能本身无效。它看起来像

template<size_t N, size_t M>
       void printPtr( int(*A)[M][N]) {
           for(int i=0; i < M; i++){
               for(int j=0; j<N; j++) {
                   cout << ( *A )[i][j] << " ";
               }
               cout << endl;
           }
       }

您必须使用表达式( *A )[i][j]而不是A[i][j]

因此,您需要按照上面显示的方式更改函数,并使用适当的指针作为参数

int (*ptrA)[4][3] = &A;
printPtr(ptrA);

当然最好将函数参数定义为对二维数组的引用。例如

template<size_t N, size_t M>
       void printPtr( int(&A)[M][N]) {
           for(int i=0; i < M; i++){
               for(int j=0; j<N; j++) {
                   cout << A[i][j] << " ";
               }
               cout << endl;
           }
       }

在这种情况下,你可以像

一样调用函数
printPtr( A );

答案 2 :(得分:0)

应该是

template<size_t N, size_t M>
void printPtr(const int(&A)[M][N]) {
   for(int i=0; i < M; i++){
      for(int j=0; j<N; j++) {
         cout << A[i][j] << " ";
      }
      cout << endl;
   }
}

致电:

printPtr(A);

template<size_t N, size_t M>
void printPtr(const int(*A)[M][N]) {
   for(int i=0; i < M; i++){
      for(int j=0; j<N; j++) {
         cout << (*A)[i][j] << " ";
      }
      cout << endl;
   }
}

致电:

printPtr(&A);

BTW你的指针应该是:

int (*ptrA)[4][3] = &A;