Questions tagged [addressof]
95 questions
95
votes
5 answers
When to use addressof(x) instead of &x?
How do I decide whether I need addressof(x) instead of &x when taking the address of an object?
Seems like the question was confusing, so a clarification is in order:
addressof obviously bypasses the overloaded address-of operator. I'm already…

user541686
- 205,094
- 128
- 528
- 886
33
votes
4 answers
What's the ampersand for when used after class name like ostream& operator <<(...)?
I know about all about pointers and the ampersand means "address of" but what's it mean in this situation?
Also, when overloading operators, why is it common declare the parameters with const?

Omar
- 750
- 3
- 7
- 13
25
votes
2 answers
Does the C preprocessor remove instances of "&*"?
I was playing around with gcc and tried the following bit of code:
int A = 42;
int *B = &A;
int *C = &*B;
And C == &A, as expected. But when I try:
int *B = NULL;
int *C = &*B;
Turns out C == NULL, and no segfault. So &*B is not actually…

evenex_code
- 843
- 1
- 8
- 20
24
votes
2 answers
Why does GCC define unary operator '&&' instead of just using '&'?
As discussed in this question, GCC defines nonstandard unary operator && to take the address of a label.
Why does it define a new operator, instead of using the existing semantics of the & operator, and/or the semantics of functions (where foo and…

Jeremy
- 5,055
- 1
- 28
- 44
24
votes
2 answers
c++ get address of variable without operator&
I have got a class that has overloaded unary operator&. The objects of that type were created using new, so address of variable was accessible but now I need to use static object. Is it possible to get its address?

Ashot
- 10,807
- 14
- 66
- 117
16
votes
5 answers
"Address of" (&) an array / address of being ignored be gcc?
I am a teaching assistant of a introductory programming course, and some students made this type of error:
char name[20];
scanf("%s",&name);
which is not surprising as they are learning... What is surprising is that, besides gcc warning, the code…

dbarbosa
- 2,969
- 5
- 25
- 29
13
votes
8 answers
Why scanf must take the address of operator
As the title says, I always wonder why scanf must take the address of operator (&).

Wazery
- 15,394
- 19
- 63
- 95
12
votes
1 answer
Why does unary operator & not require a complete type?
The following code compiles fine with both gcc 7.2.0 and clang 6.0.0.
#include
struct stru;
void func(stru& s) {
std::cout << &s << std::endl;
}
int main() {
}
I'm wondering how this is OK. What if stru has overloaded operator&()?…

Lingxi
- 14,579
- 2
- 37
- 93
11
votes
1 answer
std::addressof as a constant expression in C++17
The specification of std::addressof was changed for C++17: it is now allowed to be a constant expression. However, cppreference says that:
The expression std::addressof(E) is a constant subexpression, if E is
an lvalue constant…

Vincent
- 57,703
- 61
- 205
- 388
11
votes
1 answer
getting error: cannot take the address of an rvalue of type 'int'
I tryed to compile old code with new compiler and got the next error:
error: cannot take the address of an rvalue of type 'int'
Here is the example with 2 lines - one that compiles and the other that gives an error
struct mstct {
int myfield;
…

yehudahs
- 2,488
- 8
- 34
- 54
10
votes
3 answers
Passing AddressOf to a function in VB.NET to use AddHandler
I need to pass a reference of a function to another function in VB.NET. How can this be done?
My function needs to use AddHandler internally, for which I need to pass it a handling function. My code below obviously does not work, but it conveys the…

DieSlower
- 252
- 1
- 3
- 8
8
votes
3 answers
Address of dereferenced pointer construct
In unqlite c library I found following code:
pObj = jx9VmReserveMemObj(&(*pVm),&nIdx);
where pVm is:
typedef struct jx9_vm jx9_vm;
jx9_vm *pVm
and function called is declared as:
jx9_value * jx9VmReserveMemObj(jx9_vm *, sxu32 *);
What for…

shibormot
- 1,638
- 2
- 12
- 23
8
votes
3 answers
Is there a reason to use *& or &* in C code?
I've come across some C code that make use of the reference/dereference (or whatever you choose to call them) operators, * and &, at the same time, like &*foo and *&bar. I'm puzzled by it. Is there any reason to do this?

qsfzy
- 554
- 5
- 17
7
votes
1 answer
Determining the Parameter Types of an Undefined Function
I've recently learned that I cannot:
Take the address of an undefined function
Take the address of a templatized function with a type it would fail to compile for
But I've also recently learned that I can call decltype to get the return type of…

Jonathan Mee
- 37,899
- 23
- 129
- 288
7
votes
1 answer
Should Taking the Address of a Templatized Function Trigger its Compilation?
I got an official answer to this question that decltype should not trigger function compilation. In fact decltype on a function that is declared but not defined is legal.
Next question, should taking the address of a function trigger the compilation…

Jonathan Mee
- 37,899
- 23
- 129
- 288