Home > Blockchain >  Problems casting an address to a pointer
Problems casting an address to a pointer

Time:09-17

I understand that the address-of operator & stores the actual address of the variable. A pointer stores a reference to an address that I can access using the dereference operator *. What I am not understanding is how to make a pointer point to a given address. Non of the following have worked.

void getDouble(double &addr) {
  // Incorrect type cast error
  std::cout << *((double *)addr) << std::endl;
  
  // Incorrect type cast error
  double *dbl = addr;
  std::cout << (double *)addr << std::endl;

What is the correct way to get the value from an address in C/C ?

CodePudding user response:

I understand that the address-of operator & stores the actual address of the variable

An operator doesn't "store" anything. When you pass a value as the operand of built-in address-of operator, the resulting value is a pointer to the object. If the operand is a reference, then the result is a pointer to the referred object.

A pointer stores a reference to an address

A pointer stores an address. The value of the pointer represents the address. Using the indirection operator, you can indirect through a pointer to get an lvalue referring to the pointed object.

// Incorrect type cast error
double *dbl = addr;

addr is a reference to a double. A reference to double doesn't implicitly convert to a pointer.

// Incorrect type cast error
std::cout << *((double *)addr) << std::endl;

A reference to double doesn't convert to a pointer even explicitly.

You can use the addressof operator to get a pointer to the referred object, if that's what you want to do:

double *dbl = &addr; // pointer to object referred by addr

What is the correct way to get the value from an address in C

If you have reference such as addr in the example, then you simply use the reference just like any other value. The reference will be implicitly indirected through and the value of the referred object is accessed. For example, if you want to print the value of the object:

std::cout << addr;

Okay so it's different in C , whereas in C the & strictly means address-of

It's important to understand that tokens have different meanings in different contexts. Example:

int i = 1 & 2;
//        ^--- bitwise-and operator (binary)
int* ptr = &i;
// ^       ^-- addressof operator (unary)
// ^---------- not an operator; signifies a pointer type
int i2 = *i * 42;
//       ^  ^- multiplication operator (binary)
//       ^---- indirection operator (unary)
int& ref = i;
// ^ not an operator; signifies a reference type
int i3 = ref; // automatic indirection

C and C are separate languages. C doesn't have reference variables; the example program would be simply ill-formed. It's best to not make assumptions about one language based on knowledge of another language.

CodePudding user response:

I understand that the address-of operator & stores the actual address of the variable.

It returns the address, it does not store the address anywhere. You need a separate assignment to store that address somewhere, like in a pointer.

A pointer stores a reference to an address

No, it stores the actual address.

that I can access using the dereference operator *.

You dereference a pointer to access the data that is stored at the address which the pointer is pointing at.

What I am not understanding is how to make a pointer point to a given address. Non of the following have worked.

That is because you are not actually using the address-of & operator to begin with. double &addr declares a reference named addr that refers to a double somewhere. It does not take the address of a double named addr. A reference is just an alias, it is not a pointer (though most compilers will implement a reference using a pointer).

You need to use the actual address-of & operator (or std::addressof()), even if you are working with a reference to a variable. Taking the address of a reference returns the address of the thing being referred to.

void getDouble(double &dbl) {

  // prints the value of the double
  std::cout << dbl << std::endl;

  // prints the address of the double
  std::cout << &dbl << std::endl;

  // prints the value of the double
  std::cout << *(&dbl) << std::endl;
  
  // prints the address of the double
  double *addr = &dbl;
  std::cout << addr << std::endl;
  
  // prints the value of the double
  std::cout << *addr << std::endl;
}
  • Related