矩阵打印使用指针

Matrix Printing using Pointers

本文关键字:指针 打印      更新时间:2023-10-16

我被这个问题难住了,我真的需要一些帮助。我有一个函数Void f(int *a, int m, int n),它应该打印所有使用指针的元素。I tried

for(i = 0; i < m; ++i)
        for(j = 0; j < n; ++j)
            cout << *((a+i)+j);

,但它不打印正确的元素。请帮帮我。

在main函数中,我声明了矩阵,然后读取了元素。

int col, rand, i, j;
    int a[100][100];
    cout << " col = ";
    cin >> col;
    cout << " rand = ";
    cin >> rand;
    for(i = 0; i < rand; ++i)
        for(j = 0; j < col; ++j)
    {
        cout << "a[" << i << "]" << "[" << j << "] = ";
        cin >> *(*(a+i)+j);
    }

当我尝试从main函数中打印元素时,一切正常

for(i = 0; i < rand; ++i)
        for(j = 0; j < col; ++j)
            cout << *(*(a+i)+j);
f(*a, col, rand); // function calling in the main function

Try

cout << *(a+i*(p+1)+j);

但我认为下面也会工作。(mnrp的关系需要澄清。

for(i = 0; i <= r; ++i)
    for(j = 0; j <= p; ++j)
        cout << *a++;