Search code examples
c++pointersdereference

oid Convert a double* to double


In my code I have to use a function that accepts a double* as an argument

void function_1(int a, double* var){
  (*var) = 0.;
  for (int i=0; i<a.Size(); i++) {
    (*var)+=pow(a[i],2);
  }
  (*var) = (*var)/a.Size();
}

Then I have to use (*var) in another function that needs var as a double

double function_2(double x)

I tried to use

function_2( *(double*) var )

but without any success... Since I am new to c++ I think I am doing something fundamentally wrong. Any help on that?


Solution

  • Function 2 should accept a double all the same

    function_2( double var );
    

    and then you can call it with * like so:

    function_2(*var);
    

    Here's a great and comprehensive tutorial on pointers and references (they are very related concepts in c++). It has some images that really make you understand this topic.