cppreference.com has an interesting article about the c++ address operator. The author writes, “The address operator is not a constant expression. It behaves like one, but can have a different value depending on the current location in the program.
Why is it so hard to understand? That question is asked by a lot of people, but I think it’s more than worth asking.
The truth is that I’m not at all certain why this is that hard. For one thing, it’s an operator that is not constant, which seems to me to be one of the most basic rules of C++ programming. For another, it’s an operator that is defined outside the scope of a variable. For a third, it’s an operator that can be used with pointers to something, but not with a variable.
The C address operator isn’t defined as “a pointer can be passed to a function as an argument that’s address of a variable,” but rather that whenever you have a variable of type int, you can use the operator to set the value of that variable as an address of a pointer. And you can use it with a pointer, but you can’t use it with a variable.
Using the address operator works for pointers to functions, but it does not work for pointers to variables. So you have to pass a pointer to a function or variable and use the operator to set the reference.
I’m sure the C++ standard committee would have a bunch of other useful operators for you to use, but I think the easiest thing to understand is that these operators are intended for passing pointers to a pointer to a variable. If you’re passing a pointer to a function, it isn’t necessary to pass the address of the function so that it can be called with the address of a pointer to a variable.
This code isn’t very clear.
Of course, there are some ways to get around this limitation, but this is the most effective way to do it. In this example the address operator is used to set the reference to the pointer to a variable.
This is a question I get asked all the time. My advice to answer it is to see if the code youre writing can be optimized to a point where it can be written in a way that avoids the address operator. If not, it might be a sign that your code isnt optimized.