c++ - 在此示例中,指向指针的指针有什么作用?

标签 c++ pointers

在网上搜索得知,指向指针的指针指的是存储地址的指针。但是我没有在表格中使用它们得到这个例子。是不是像 **tab 一样可以查看 *[rows] ,这是每行的值?

将**tab改成简单的tab会导致程序无法运行。

#include <iostream>

using namespace std;

void write_elements_of_the_table(int **T,int rows, int columns) {

    for (int i=0;i<rows;i++){
    cout <<"\t["<<i<<"]";
    }

    cout<<endl;

    for (int i=0;i<rows;i++){
        cout <<"["<<i<<"]";
       for (int j=0;j<columns;j++){
        cout <<"\t  "<< T[i][j];
       }
    cout<<endl;
    }
}


int main()
{ 
    int **tab, columns,rows;
    cout<<"Write the amount of rows:"<<endl;
    cin >> rows;
    cout<<"Write the amount of columns"<<endl;
    cin >> columns;
    // pointer table
    tab = new int *[rows];
    for (int i=0;i<rows;i++){
      tab[i]=new int[columns];
    }

    for (int i=0;i<rows;i++){
       for (int j=0;j<columns;j++){
        cout<<"Write the element value"<<endl;
        cin>>tab[i][j];
       }

    }

    write_elements_of_the_table(tab,rows,columns);

    //deleting the table

    for (int i=0;i<rows;i++){
        delete []tab[i];
    }

    delete []tab;

    return 0;
}

我想了解**T和**tab的含义。

最佳答案

I would like to understand the meaning of the **T and **tab.

int 是一种类型。更具体地说,它是一个整数类型。

int* 也是一种类型。更具体地说,它是一个指针类型。指针指向其他对象。 int* 类型的对象特别指向int 类型的对象。

int** 也是指针类型。它也指向其他对象。 int** 类型的对象指向int* 类型的对象。 int **Tint **tabint** 类型的变量。


new[] 表达式分配具有动态存储的对象数组。表达式返回的值是指向该数组第一个元素的指针。

new int[n] 分配一个 int 对象数组。表达式返回的值是指向该数组中第一个对象的指针。该指针的类型是 int*

new int*[m] 分配一个 int* 对象数组。表达式返回的值是指向该数组中第一个对象的指针。该指针的类型是 int**

tab的简明描述是:tab是一个指向指针数组首元素的指针,其中数组的每个元素都指向一个指针数组的首元素整数数组。

关于c++ - 在此示例中,指向指针的指针有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55890770/

相关文章:

c++ - 减少通过 UDP 套接字发送的数据

C++ 指针延迟声明语法

c - 将结构的指针成员传递给函数时出现错误 )

c++ - 取消引用存储在寄存器中的指针(Visual Studio)

c++ - C++ 中 += 和 =+ 的区别

vb.net - VB.NET 中 C 指针的等价物是什么?

java - 打印桌面的屏幕截图并转换为像素矩阵 [0,1]

c++ - 为什么我的 DirectInput8 堆栈会溢出?

c++ - 分配非 const 变量的只读位置

c++ - 解析两种格式的文件并解析行