My C++ course hasn't covered a topic yet, but I'm expected to solve a few problems with this material. I've got some working code I just don't fully understand it. Can someone help me, please?

void f(int** a) //What is this ** business?  (1)
{
    cout << **a; //prints the value stored in a, output is 45
    cout << *a;   //so what does this print?  (2)
    cout << &a;   //confused here too    (3)
}

//I'm OK with main(). I'm including it FYI
int main()
{
    int i = 45;
    int* ptr = &i;
    f(&i);
}

I need help understanding lines (1), (2), & (3).

Thank you in advanced for your help.

Recommended Answers

All 3 Replies

void f(int** a) //its a pointer to a pointer. The argument to the function f is a pointer to pointer
{
    cout << **a; //prints the value stored in a, output is 45
    cout << *a;   //address of the memory where 45 is stored
    cout << &a;   //address of variable 'a' (it will come as ***)
}

//I'm OK with main(). I'm including it FYI
int main()
{
    int i = 45;
    int* ptr = &i;
    f(&i);/*u r passing incorrect arg*/
             /*passing 'int *' instead of 'int **'*/
              /*should have called as:  f(&ptr);*/
}

EDIT: correction
line #5: its "int ***" and not simply "***" in the comment.

commented: Thank you +1

Thanks for the explanation. Much appreciated.

u are welcome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.