Search code examples
c++functionpass-by-referencepass-by-value

Difference between passing value in function by int ** a and int & a


Is there any difference if i pass int **a in any function and at same place i pass int& a, will both create any difference?. Ex

Bool issafe(int**arr, intx, int y)

Bool issafe(int& arr, intx, int y)

Solution

  • There is a lot of difference between the '*' notation and '&' notation.

    Let us have a look at what both mean, in a general sense and little bit of technical detail.

    Reference Operator (&)

    &x simply means Address of x. It will be a an address value, often represented in hexadecimal notation.

    It is often used to pass by reference in functions: func(int &x, char a); This will pass x by reference, but a by value.

    Dereference Operator (*)

    *p means Pointer to a memory address. We may assign an address to this pointer using the following format:

    int *p = x

    or

    int *p; p = &x

    Once the pointer is pointing to an address, *p and x can be used interchangeable through the rest of the program.

    Double Dereferencing

    Usage of ** occurs when we need a pointer to a pointer.

    For example, if we need to make a a 2D Array, we can use the ** notation.

    One pointer points to the first element of the first row in the matrix, while the other pointer points to the first row in the list of rows.

    Thus, to answer your question in a few conclusive words: Yes, there is a big difference in int ** a and int & a, so much so that they go one level beyond being opposites like * and &.