reference to indirection to pointer

Write your question here.

1
2
3
4
5
6
7
8
9
10
int main(){
  
  int a=5;
  int* q=a;
  int& p= *q;

return 0;




first of all sorry for my english. I don't understand how reference works in the example above. *q gives the value pointed by q, it should be 5.But p needs " int variable ", not a value.

in the classic example
1
2
3
4

int p;
int *q=p;


the q address is the same of p address, so in the first example
p address is the same of " *p " address, but it should be a value. it isn't a variable.
what is the address of a number?
thank you
Neither of your examples compile, because you are attempting to convert an int to a pointer-to-int.

First example:
Line 3: You declare a variable called 'a'.
Line 4: You attempt to assign an int (a) to a pointer (q).

Second example is same thing. You are trying to assign an int to a pointer-to-int. These are different data types.

If you have an int variable, and need a pointer to it, you do:
1
2
int a = 5;
int* ptr = &a; //&a here means "address of" a 


what is the address of a number?
It's best to use the correct terminology. A 'number' does not have an address. 42 is an int literal; it does not have an address by itself. A variable can have an address. A variable could be an int, a double, a string, etc. The address of a variable is where that variable is located in memory.

In other words, a variable stores some value, and has an address.

______________________________________________


References are a different concept. When & is with the type, it means something different. It means that the variable is a reference, or alias, of another object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Example program
#include <iostream>

int main()
{
  int a = 5;
  int* ptr= &a;

  int& a_ref = *ptr;
  a = 6;
  
  std::cout << a_ref << '\n';

  return 0;
}


In this code, 'a' and 'a_ref' both refer to exactly the same object.

What does this print out?
Last edited on
this prints out 6. l thank you really much, but l don't understand .

you can do

1
2
3

const int& p= 4;


to do it , you need to put const before int, because 4 is a number.
so why you dont' have to put const before int in the following statement
 
int& a_ref=* ptr;


in my opinion * ptr is the value of a , that is a number, too .It is the value at the address available in the pointer variable


I'll try to explain, but someone else probably will be able to do a better job.

First, the const int& thing. Doing const type& is special because it can extend the lifetime of a temporary created, to allow it to still be viewed as a reference to an object (but you can't change this object because it's const).

in my opinion * ptr is the value of a , that is a number, too
The issue is the loose usage of the word "number" here. Yes, *int_ptr (dereferenced int pointer) is a number, but the term "number" isn't very useful here.

I'm simplifying this a bit, but basically: there are 'rvalues' and 'lvalues'. An l-value is something that you can take the address of. Keep this in mind.

42 is an int literal. You cannot take the address of 42. That is, you cannot do int* ptr = &42;. Therefore, 42 is not an lvalue. Likewise, you can't do int& int_ref = 42;. 42 will always be 42, and 42 doesn't have an address.

When you do const int& int_ref = 42;, internally the compiler does something like this:
1
2
int __special_internal_variable = 42; // copy the value 42 into the variable.
const int& int_ref = __special_internal_variable; // now, you can take a reference of an lvalue 


If this doesn't make sense to you, don't worry about it. Just note that const int& is special and allows you to still reference a temporary.

__________________________________

When you do
1
2
int a = 42;
int* ptr = &a;

ptr is now the address of your a variable.
When you do (*ptr), you dereference your address, and you get a reference to the value at that address.

This is what allows you to say int& a_ref = *ptr;

Now, a_ref is exactly the same object as a.
Last edited on
you are really very kind. it is almost clear.
l understand difference between lvalue and rvalue.
just one thing if l am not too boring..
could you tell what internally compiler does in the particular case

 
int& a_ref=*ptr;


if you can do it, it would be very useful to me. l am curious about behaviour internally.
thank you so much.


Last edited on
I'm not sure how to answer that. In that particular case, I don't think there's a useful analogy.

- ptr points to some object which has an address.
- *ptr dereferences the address to reference this object.
- The assignment to a_ref now lets you directly refer to the same object that ptr points to.
Last edited on
1
2
3
int a = 5;
int* q = &a;
int& p = *q;

I think the easiest way to think about this is that *q returns a reference (int&) to a. Changing the value of a will change the value you read through p.
Last edited on
l thank you everybody.
f
from a strange point of view( l know it is not correct, just to understand) in this case

*p is an alias for a ?
Yes. It's just a roundabout way of writing:
1
2
int a = 5;
int& p = a;
1
2
3
int a = 5;
int* q = &a;
int& p = *q;


*p is an alias for a ?


Almost, but not quite.

*q is an alias for a.
p is another alias for a.

The variable p is not a pointer, so *p is meaningless.
Topic archived. No new replies allowed.