Garima Singh
Garima Singh

Reputation: 1490

What is C-equivalent of reference to a pointer "*&"

Could someone please let me know the C-equivalent of reference to a pointer "*&"?

In other word, if my function is like this in C++:

void func(int* p, int*& pr)
{
    p++;
    pr++;
}

How would I changed the second argument while converting it in C?

UPDATE: @MikeDeSimone : Please let me know if I understood the translated code properly? Let me start by initializing variable:

int i = 10;
int *p1 = &i;
int **pr= &p1;

So, when you performed (*pr)++ , that is basically equivalent to:

(p1)++

However, I fail to understand how would that look from inside main()?

Question 2: what would I do if I have code snippet like this?

void pass_by_reference(int*& p)
{
        //Allocate new memory in p: this change would reflect in main
    p = new int;
}

Upvotes: 0

Views: 6930

Answers (1)

Mike DeSimone
Mike DeSimone

Reputation: 42825

You use a pointer to a pointer.

void func(int* p, int** pr)
{
    p++;
    (*pr)++;
}

See, for example, the second parameter to strtoul, which the function uses to return the point at which parsing stopped.


Sorry for the late update...

Please let me know if I understood the translated code properly? Let me start by initializing variable:

int i = 10;
int *p1 = &i;
int **pr= &p1;

So, when you performed (*pr)++ , that is basically equivalent to:

(p1)++

Yes.

However, I fail to understand how would that look from inside main()?

I don't understand how main comes into this; we were talking about func. For this discussion, main would be a function like any other. Variables declared within a function only exist during execution of that function.

Question 2: what would I do if I have code snippet like this?

void pass_by_reference(int*& p)
{
        //Allocate new memory in p: this change would reflect in main
    p = new int;
}

The thing to remember about references passed into functions is that they are just saying "this parameter is a reference to the parameter passed to the function, and changing it changes the original. It is not a local copy like non-reference parameters."

Reviewing references in practice:

  • If your function is declared void func(int foo); and called with int k = 0; foo(k); then a copy of k is made that func sees as foo.
    • If func changes foo, k does not change. You will often see functions "trash" their passed-in-by-copy parameters like this.
  • If your function is declared void func(int& foo); and called with int k = 0; foo(k); then a reference to k is made that func sees as foo.
    • If func changes foo, it is actually changing k.
    • This is often done to "pass back" more values than just the return value, or when the function needs to persistently modify the object somehow.

Now the thing is that C doesn't have references. But, to be honest, C++ references are C pointers under the hood. The difference is that references cannot be NULL, should not be taken as pointing to the start of a C array, and references hide the pointer operations to make it look like you're working on the variable directly.

So every time you see a reference in C++, you need to convert it to a pointer in C. The referred-to type does not matter; even if it's a pointer, the reference turns into a pointer itself, so you have a pointer-to-pointer.

So what's a pointer, anyway? Remember that memory is just a big array of bytes, with every byte having an address. A pointer is a variable that contains an address. C and C++ give pointers types so the language can determine what kind of data the pointer is pointing to. Thus an int is an integer value, and an int* is a pointer to an integer value (as opposed to a pointer to a character, or structure, or whatever).

This means you can do two general things with a pointer: you can operate on the pointer itself, or you can operate on the object the pointer is pointing to. The latter is what happens when you use unary prefix * (e.g. *pr) or -> if the pointer points to a structure. (a->b is really just shorthand for (*a).b.)

Upvotes: 6

Related Questions