dimanche 18 avril 2021

Why is a pointer variable not an l-value?

I am learning about l-values and r-values. I understand an l-value to be: an object that occupies a location in memory and it is addressable.

Why is a pointer expression not considered a l-value?

With the code below:

int main(){
  int x { 100 };
  int* ptr = &x;
  int& l_val_ref = ptr; //problem on this line.
  cout << l_val_ref<< endl;
  return 0; 
}

I get an error saying:

a reference of type "int &" (not const-qualified) cannot be initialized with a value of type "int *"C/C++(434)

I know that int& is not the same type as int*.

May you please explain this to me. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire