C++ 制作一个接受数组输入的函数,打印2d c++;

C++ 制作一个接受数组输入的函数,打印2d c++;,c++,arrays,C++,Arrays,作用域:创建一个函数,该函数将2d数组作为输入,并单独打印每个元素 我从函数中的数组开始,这非常有效 #include<iostream> void printArray(); int main() { printArray(); return 0; } void printArray() { int array[4][2] = {{1,2},{3,4},{5,7},{8,9}}; int rows = sizeof(array)/sizeof(array[0])

作用域:创建一个函数,该函数将2d数组作为输入,并单独打印每个元素

我从函数中的数组开始,这非常有效

#include<iostream>
void printArray();
int main()
{
  printArray();
  return 0; 
}

void printArray()
{
  int array[4][2] = {{1,2},{3,4},{5,7},{8,9}};
  int rows = sizeof(array)/sizeof(array[0]);
 int cols = sizeof array[0] / sizeof array[0][0];
 for (int i = 0 ; i < rows ; i++)
   {
     for ( int j = 0; j < cols; j++ )
       {
     std::cout<<array[i][j]<<' ';
       }
     std::cout<<'\n';
   }

 std::cout<<rows<<"    "<<cols;

}

刚刚修改了您的代码并注释了需要更改的地方:

#include<iostream>

// as you are passing an array to your function,
// this is one valid syntax to do so:
void printArray(int array[4][2], int rows, int cols);

int main()
{
  int arr[4][2] = {{1,2},{3,4},{5,7},{8,9}};

  // You can't get sizeof arrays that have been passed to
  // a function. Functions accept them as pointers
  // and sizeof(arr) inside the function will actually
  // return the sizeof pointer.
  int rows = sizeof(arr)/sizeof(arr[0]);
  int cols = sizeof(arr[0])/ sizeof(arr[0][0]);

  printArray(arr, rows, cols);
  return 0;
}

void printArray(int array[4][2], int rows, int cols)
{

  for (int i = 0 ; i < rows ; i++)
   {
     for ( int j = 0; j < cols; j++ )
       {
     std::cout<<array[i][j]<<' ';
       }
     std::cout<<'\n';
   }

 std::cout<<rows<<"    "<<cols;

}
#包括
//在向函数传递数组时,
//这是一种有效的语法:
void printary(int数组[4][2],int行,int列);
int main()
{
int arr[4][2]={{1,2}、{3,4}、{5,7}、{8,9};
//无法获取已传递给的数组的大小
//函数。函数接受它们作为指针
//函数中的sizeof(arr)实际上
//返回sizeof指针。
int rows=sizeof(arr)/sizeof(arr[0]);
int cols=sizeof(arr[0])/sizeof(arr[0][0]);
打印阵列(arr、行、列);
返回0;
}
void printary(int数组[4][2],int行,int列)
{
对于(int i=0;istd::cout传递的是整数而不是数组

有3种方法可以将二维数组作为参数传递

方法1: 将其作为包含int的指针传递

int *arr[4];
for (i = 0; i < 2; i++)
    arr[i] = new int[3]
void printArray(int *arr[4]) { ... }

你的函数要求使用
int
类型,而不是int数组类型谢谢你的评论,这非常有用,除了你提供的阅读材料之外,我还发现了一个youtube视频,解释了这一点,我觉得很有帮助。
#include<iostream>

// as you are passing an array to your function,
// this is one valid syntax to do so:
void printArray(int array[4][2], int rows, int cols);

int main()
{
  int arr[4][2] = {{1,2},{3,4},{5,7},{8,9}};

  // You can't get sizeof arrays that have been passed to
  // a function. Functions accept them as pointers
  // and sizeof(arr) inside the function will actually
  // return the sizeof pointer.
  int rows = sizeof(arr)/sizeof(arr[0]);
  int cols = sizeof(arr[0])/ sizeof(arr[0][0]);

  printArray(arr, rows, cols);
  return 0;
}

void printArray(int array[4][2], int rows, int cols)
{

  for (int i = 0 ; i < rows ; i++)
   {
     for ( int j = 0; j < cols; j++ )
       {
     std::cout<<array[i][j]<<' ';
       }
     std::cout<<'\n';
   }

 std::cout<<rows<<"    "<<cols;

}
int *arr[4];
for (i = 0; i < 2; i++)
    arr[i] = new int[3]
void printArray(int *arr[4]) { ... }
int **arr;
arr = new int *[4];
for (i = 0; i < 2; i++)
    arr[i] = new int[3];
void printArray(int **arr) { ... }
int arr[4][2] = {{1,2},{3,4},{5,7},{8,9}};
void printArray(int arr[][2]) { ... }