Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generally, is dereference pointer expression results a reference type?

Deferencing pointer leads to using the value of the object indirectly. But I've never really understood what does the "using" means. I started to think the question until my compiler yield an error for the following code

int i = 0, *pi = &i;
decltype(*pi) c; // error: 'c' declared as reference but not initialized.

I looked at the error for a very long time and searched some questions I can only give out the following arguments. I don't know if they are correct or not.

Arguments 1:

1) *p is an expression that is not a variable (or non-variable expression)

2) dereferencing pointer expression yields a reference, we are in fact using a reference to access the value of the object

Arguments 2:

the dereferencing expression only for which decltype returns a reference, it is not a general case

Please points out any incorrectness or inaccurate descriptions of the above arguments.

like image 914
SLN Avatar asked Jan 22 '18 18:01

SLN


People also ask

What does dereference a pointer mean?

Dereferencing a pointer means getting the value that is stored in the memory location pointed by the pointer. The operator * is used to do this, and is called the dereferencing operator.

What is reference and dereferencing?

I read about * referencing operator and & dereferencing operator; or that referencing means making a pointer point to a variable and dereferencing is accessing the value of the variable that the pointer points to.

What happens when we dereference a pointer?

Dereferencing is used to access or manipulate data contained in memory location pointed to by a pointer. *(asterisk) is used with pointer variable when dereferencing the pointer variable, it refers to variable being pointed, so this is called dereferencing of pointers.

Which operator is used to dereference a pointer?

The dereference operator is also known as an indirection operator, which is represented by (*). When indirection operator (*) is used with the pointer variable, then it is known as dereferencing a pointer.


1 Answers

Dereferencing a pointer yields an lvalue expression of the pointed-to type designating the object or function pointed to. It does not yield a reference.**pi is an lvalue of type int.

decltype (with an exception not relevant here) reports both an expression's type and its value category, the latter being encoded with reference types. Since *pi is an lvalue, it's encoded as an lvalue reference type, so decltype(*pi) is int &: int for the type, & for the value category.

Expressions never have reference type because any referenceness is adjusted away "prior to any further analysis".


* This isn't a merely technical distinction: per the direction of core issue 232 and core issue 453, there are valid dereference expressions you can write where binding its result to a reference would cause undefined behavior.

like image 134
T.C. Avatar answered Sep 18 '22 14:09

T.C.