1

I'm having a problem with a C++ program involving two dimensional arrays.

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.

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++.

3
  • 1
    When you say I must use double pointer to int, is this meant as a constraint? If not, I would definitely recommend using vectors. A 2D one suffices when you don't need the extra speed or elegance from a wrapped 1D one. Anyway, the size of a pointer will always be the same. Commented Sep 9, 2012 at 4:03
  • 1
    Is there any reason you can't use vector or boost::multi_array for this? Commented Sep 9, 2012 at 4:05
  • Chris, Brendan Long: I must use double pointers since it's a school project, and using double pointers is requirement. Commented Sep 9, 2012 at 15:02

1 Answer 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:

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.

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It doesn't really make sense to compare containers to pointers though. Where a function would idiomatically accept a pair of pointers or a (pointer, size) pair in C, a function can accept a pair of iterators in C++. (Keeping in mind that a pair of pointers is a pair of iterators.)
shf301: Ok, thanks, I guess I'll have to include the rows and columns as function parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.