Passing a pointer to a pointer to a function.

This is a discussion on Passing a pointer to a pointer to a function. within the C++ Programming forums, part of the General Programming Boards category; I'm having minor difficulties understanding pointers and passing them to functions. The program I am trying to make dynamically allocates ...

  1. #1
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40

    Passing a pointer to a pointer to a function.

    I'm having minor difficulties understanding pointers and passing them to functions. The program I am trying to make dynamically allocates a two dimensional array assigned to a pointer to a pointer and then I want to pass the pointer to a function I have made. The code is:

    Code:
    #include <iostream>
    
    
    using namespace std;
    void printTable (int p_p_table, int width, int height);
    void createTable (int width, int height);
    
    
    int **p_p_table;
    
    
    int main()
    {
        int width, height;
        cout << "Please enter the width followed by the height of the table: ";
        cin >> width >> height;
        createTable(width, height);
        printTable(**p_p_table, width, height);
    
    
    
    
    
    
        return 0;
    }
    
    
    void createTable(int width, int height)
    {
        p_p_table = new int* [width];
        for (int i = 0; i < width; i++)
        {
            p_p_table = new int* [height];
        }
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                p_p_table [i][j] = i * j;
            }
        }
    
    
    }
    
    
    void printTable(int **p_p_table, int width, int height)
    {
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                cout << p_p_table [i][j] << " ";
            }
            cout << "\n";
        }
    }
    When I try to compile it I get an error message: line 15; undefined reference to 'printTable (int, int, int)', and I dont really know what it means, and googling the answer gave answers giving the same error line, but not having the same code.
    Last edited by Iceboxes; 12-11-2013 at 04:14 AM.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,342
    In C++, printTable(int, int, int) is a different function than printTable(**int, int, int), you didn't include the pointer to pointer in the prototype.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40
    removing the two asterisks from when I call the function gives me the error: line 15; invalid conversion from 'int**' to 'int'.
    Adding to asterisks to the declaration gives me a simililar error: line 15; invalid conversion from 'int' to 'int**'.

    This is the main function:
    Code:
    int main()
    {
        int width, height;
        cout << "Please enter the width followed by the height of the table: ";
        cin >> width >> height;
        createTable(width, height);
        printTable(**p_p_table, width, height);
    
    
    
    
    
    
        return 0;
    }
    Here is the function:
    Code:
    void printTable(int **p_p_table, int width, int height)
    {
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                cout << p_p_table [i][j] << " ";
            }
            cout << "\n";
        }
    }
    Here is the declarations:
    Code:
    using namespace std;
    void printTable (int **p_p_table, int width, int height);
    void createTable (int width, int height);
    
    
    int **p_p_table;

  4. #4
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40
    What am I missing?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,671
    The asterisks have different meaning when declaring a pointer to pointer and when using it. The declaration "int **p_p_table" tells the compiler that **p_p_table is an int. When using a pointer to pointer, for example, "printTable(**p_p_table, width, height)" the double asterisks double-dereference p_p_table to obtain the int.

    Assuming, of course, that p_p_table has been initialised so it points to a valid pointer that points to a valid int. You have not shown any code which does that.
    Last edited by grumpy; 12-11-2013 at 06:24 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40
    In my first post I assigned int* [width] and int* [height] for every width to **p_p_table. Is this not valid? If this is not valid, could anyone please show me how I am supposed to initialise it properly?

    Maybe it will be easier to help me if I give you the purpose of the program. The purpose is to make a multiplication table letting the user define width and height, and then dynamically allocate a two dimensional array to store the table's values. And ofcourse, print it.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,030
    Well, there are several mistakes...

    First off, declaring a variable as int** means that you are creating a pointer that points to int*. A pointer must point somewhere, else it cannot be dereferenced (get what it points to). So a "C" dynamic array has a pointer to the first element of an array whose every element is a pointer to the first element in another array. So if you dereference the first pointer, you get the first element of an array of int*, then if you dereference that, you get the first int in that array. Tell me how you can print the array of you fetch one of those ints? I don't know, the compiler doesn't know, the code doesn't know.

    Secondly, you should avoid global variables. Make your variable a parameter, or perhaps even a return value.

    Thirdly, you don't delete what you new, so you get memory leaks.

    And finally, C++ isn't so barbaric so you have to do all this complexity just to get a dynamic 2d array. We have what's called a vector. If you google for 2d vector, I'm pretty sure you will find some good example on how to dynamically allocate a 2d array quickly and easily.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing a function pointer
    By Akkernight in forum C++ Programming
    Replies: 17
    Last Post: 03-22-2009, 05:41 AM
  2. c ++ passing a pointer into a function
    By Ron in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2008, 09:31 PM
  3. passing method pointer as a function pointer
    By sevcsik in forum C++ Programming
    Replies: 5
    Last Post: 12-30-2007, 06:19 AM
  4. Replies: 4
    Last Post: 11-05-2006, 02:57 PM
  5. Passing Pointer to a function
    By Nishant Ghai in forum C Programming
    Replies: 4
    Last Post: 02-27-2003, 06:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21