-2

I recently came across this c++ example from cppinstitute and I have no idea how the conversion from int* to int& works.

the output of f2 is a pointer, however, f3 takes an int& how is the pointer turned into a variable so that f3 can take it by reference?

to be more precise, how can we write it in 3 lines?

the main code :

#include <iostream>
using namespace std;

int f1(int *a) {
    return *a + 1;
}

int *f2(int *a) {
    return a + 1;
}

int *f3(int &a) {
    return &a + 1;
}

int main() {
    int t[] = {0, 1, 2, 3};
    cout << f1(f3(*f2(t)));
    return 0;
}

how to assign the return values and pass them like this ?

int * a = f2(t);
int * b = f3(a);  /*some kind of conversion is hapenning here*/
int c = f1(b);

cout << c;

the above code won't compile and the compiler complains that a is an int* not an int. dereferencing a and passing it like f3(*a) will compile however won't produce the same result as the original code.

edit I made a mistake f3(*a) will compile and produce the same result. thanks eerorika

daniel
  • 21
  • 5
  • I think, you might benefit for reading a good C++ book. You can find curated list here: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list I recommend avoiding dubious online courses, certifications and tutorials when it comes to C++ - they are often of questionable quality. – SergeyA Jul 15 '19 at 21:01
  • "*how is the pointer turned into a variable*" - with `*`, as always. – melpomene Jul 15 '19 at 21:03
  • "*`f3(*a)` will compile however won't produce the same result as the original code*" - I just tried it. It produces exactly the same output. – melpomene Jul 15 '19 at 21:07

2 Answers2

2

how is the pointer turned into a variable so that f3 can take it by reference?

It is not "turned into a variable". The pointed object is accessed by indirecting through the pointer:

f3(*f2(t))
   ^ look at this

That is the indirection operator. When applied to a pointer, the result is a reference to the pointed object.


how to assign the return values and pass them like this ?

int * b = f3(a);  /*some kind of conversion is hapenning here*/

the above code won't compile and the compiler complains that a is an int* not an int

You need to indirect through the pointer:

int * b = f3(*a);
             ^ look at this

dereferencing a and passing it like f3(*a) will compile however won't produce the same result as the original code.

It will produce the same result as the original code.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • thanks for the fast reply, i'm a C coder and new to c++, in C we call the * dereference operator which i googled and is the same. – daniel Jul 15 '19 at 21:18
  • @user2679808 It's called indirection operator also in the C language. Dereference operator is another, perhaps more colloquial name for the same thing, but indirection is the standard nomenclature. See for example the section of C standard titled *Address and indirection operators* which says *The unary * operator denotes indirection*. – eerorika Jul 15 '19 at 21:28
0

The t variable is a pointer to the beginning of the array, so *t == t[0] == 0. f2() accepts a pointer to an integer, and returns a pointer to the next element in the array. Since f3() takes a reference to an integer, the pointer returned from f2() is dereferenced, and then its address (i.e. the address t+1) is passed to f3(). f3() then returns the address of integer after the a argument that is passed to it by taking the address of a and incrementing it. Finally, f1() receives a pointer to an integer, dereferences it and adds 1, and then returns the resulting value.

There are no implicit conversions in the example, just a mix of passing pointers and references. It would be helpful to review a C++ book, specifically a section about references. The difference between the two is only syntactic, and can indeed be confusing to beginners. Best of luck!

itamar reif
  • 139
  • 2
  • 9