接受来自函数的二维数组(int 类型)并将其返回到主函数



我只想将二维数组的大小作为函数中的参数,接受数组并以某种方式将其放入主函数中以执行进一步的任务。我尝试观看一些视频和帖子,但没有得到答案。请一如既往地帮助并带我摆脱这个问题。

int *accept(int a,int b)
{
   int x[50][50];
 for(int i=0;i<a;++i)
 {
  cout<<"Enter elements for "<<i+1<<" th row";
   for(int j=0;j<b;++j)
   {
    cout<<"Enter "<<j+i<<" th element n";
    cin>>x[i][j];
   }
 }
 return *x;
 }
 void main()
 {
 int arr[50][50],m,n;
 cout<<"Enter the no of rows you want : ";
 cin>>m;
 cout<<"Enter the no of columns you want : ";
 cin>>n;
 arr[50][50]=accept(m,n); //how to copy?
您可以使用

memcpy()

#include <iostream>
#include <cstring>
using namespace std;
void accept(void* arr, int a, int b)
{
     int x[a][b];
     for (int i = 0; i < a; ++i)
     {
        cout << "Enter elements for " << i+1 << " th row";
        for (int j = 0; j < b; ++j)
        {
            cout << "Enter " << j+i << " th element n";
            cin >> x[i][j];
        }
     }
     memcpy(arr, x, sizeof(int) * a * b);
 }
int main()
{
    int m, n;
    cout << "Enter the no of rows you want : ";
    cin >> m;
    cout << "Enter the no of columns you want : ";
    cin >> n;
    int arr[m][n];
    accept(&arr, m, n);
    return 0;
}

最新更新