I'm having a problem with a C++ program involving two dimensional arrays.
我遇到涉及二维数组的C ++程序的问题。
As part of the program I have to use a function which accepts as parameters two tables and adds them, returning another table.
作为程序的一部分,我必须使用一个函数,它接受两个表作为参数并添加它们,返回另一个表。
I figured I could do something like this:
我想我可以这样做:
int** addTables(int ** table1, int ** table2)
{
int** result;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
result[i][j] = table1[i][j] + table2[i][j];
}
}
return result;
}
but I don't know how to find out the size of the table (rows and columns) for my "for" loops.
但我不知道如何找出我的“for”循环的表(行和列)的大小。
Does anybody have an idea of how to do this?
有人知道怎么做吗?
This is part of the code I was testing, but I'm not getting the right number of columns and rows:
这是我正在测试的代码的一部分,但我没有得到正确数量的列和行:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(int argc, char **argv)
{
const int n = 3; // In the addTables function I'm not supposed to know n.
int **tablePtr = new int*[n]; // I must use double pointer to int.
for (int i = 0; i < n; i++)
{
tablePtr[i] = new int[n];
}
srand((unsigned)time(0));
int random_integer;
for(int i = 0; i < n; i++) // I assign random numbers to a table.
{
for (int j = 0; j < n; j++)
{
random_integer = (rand()%100)+1;
tablePtr[i][j] = random_integer;
cout << tablePtr[i][j] << endl;
}
}
cout << "The table is " << sizeof(tablePtr) << " columns wide" << endl;
cout << "The table is " << sizeof(tablePtr) << " rows long" << endl;
return 0;
}
I appreciate any help, and please keep in mind that I'm new to C++.
我感谢任何帮助,请记住我是C ++的新手。
1 个解决方案
#1
4
There is no way to "find" the size of what a pointer points to in C or C++. A pointer is just an address value. You would have to pass in the size - or in your case the number of rows or columns into the addTables
function - like:
没有办法“找到”指针在C或C ++中指向的大小。指针只是一个地址值。您必须传入大小 - 或者在您的情况下将行数或列数传入addTables函数 - 如:
int** addTables(int ** table1, int ** table2, int rows, int columns)
This is why the commentors are suggesting something like a vector
. C++ offers better data types than raw pointers - for one thing a vector tracks the number of items that it contains so it doesn't have to be passed as separate parameters.
这就是评论家提出类似矢量的原因。 C ++提供比原始指针更好的数据类型 - 一方面,矢量跟踪它包含的项目数,因此它不必作为单独的参数传递。
In your example program, the sizeof
operator returns the size of the type of the variable supplied. So for sizeof(tablePtr)
it returns the size of an int**
which will likely be 4 or 8 bytes. The sizeof
operation is evaluated a compile time so there is no way it could know how large the buffer that tablePtr
points to is.
在示例程序中,sizeof运算符返回所提供变量类型的大小。因此对于sizeof(tablePtr),它返回一个int **的大小,可能是4或8个字节。 sizeof操作在编译时进行评估,因此无法知道tablePtr指向的缓冲区有多大。
#1
4
There is no way to "find" the size of what a pointer points to in C or C++. A pointer is just an address value. You would have to pass in the size - or in your case the number of rows or columns into the addTables
function - like:
没有办法“找到”指针在C或C ++中指向的大小。指针只是一个地址值。您必须传入大小 - 或者在您的情况下将行数或列数传入addTables函数 - 如:
int** addTables(int ** table1, int ** table2, int rows, int columns)
This is why the commentors are suggesting something like a vector
. C++ offers better data types than raw pointers - for one thing a vector tracks the number of items that it contains so it doesn't have to be passed as separate parameters.
这就是评论家提出类似矢量的原因。 C ++提供比原始指针更好的数据类型 - 一方面,矢量跟踪它包含的项目数,因此它不必作为单独的参数传递。
In your example program, the sizeof
operator returns the size of the type of the variable supplied. So for sizeof(tablePtr)
it returns the size of an int**
which will likely be 4 or 8 bytes. The sizeof
operation is evaluated a compile time so there is no way it could know how large the buffer that tablePtr
points to is.
在示例程序中,sizeof运算符返回所提供变量类型的大小。因此对于sizeof(tablePtr),它返回一个int **的大小,可能是4或8个字节。 sizeof操作在编译时进行评估,因此无法知道tablePtr指向的缓冲区有多大。