在网上搜索得知,指向指针的指针指的是存储地址的指针。但是我没有在表格中使用它们得到这个例子。是不是像 **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 **T
和 int **tab
是 int**
类型的变量。
new[]
表达式分配具有动态存储的对象数组。表达式返回的值是指向该数组第一个元素的指针。
new int[n]
分配一个 int
对象数组。表达式返回的值是指向该数组中第一个对象的指针。该指针的类型是 int*
。
new int*[m]
分配一个 int*
对象数组。表达式返回的值是指向该数组中第一个对象的指针。该指针的类型是 int**
。
tab
的简明描述是:tab
是一个指向指针数组首元素的指针,其中数组的每个元素都指向一个指针数组的首元素整数数组。
关于c++ - 在此示例中,指向指针的指针有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55890770/