简体   繁体   中英

Error "double** to double*" while passing pointer in a function c/c++

I have a function that takes a double *result . I am aware that pointers need to be passed by reference in a function. When I call the function void ComputeSeriesPointer(double x, int n, double *result); in main with ComputeSeriesPointer(x, n, &result); , I get the error:

cannot convert ‘double**’ to ‘double*’ for argument ‘3’ to ‘void ComputeSeriesPointer(double, int, double*)’                            
 ComputeSeriesPointer(x, n, &result);                                                                                                                                                  
                                   ^

When working with pointers, aren't they all passed using the & key? The in class examples were all done this way. Even on the internet things were done this way. Any explanation/clarification would be great.

I am also running this with a c++ compiler (as instructed by my professor) because I am using the pow function.

I'm not sure about what you are doing without seeing the complete code, but If you are doing something like this:

void ComputeSeriesPointer(double, int, double*){
    // ...
}

int main(){
    double *x = ...;
    ComputeSeriesPointer(1.0, 1, &x); 
    // ...
    return 0;
}

Then, the problem is the &x . The & operator is used to extract a variable address. In this case, your variable is already a pointer, so writing &x you are getting a "pointer to pointer", in other words, a double** . That's your problem. Call your function in this way: ComputeSeriesPointer(1.0, 1, x)

The function is expecting you to pass the memory address of an actual double variable, not the memory address of a double* pointer variable:

double result; // NOT double*!
ComputeSeriesPointer(x, n, &result);

You can also do this:

double result; // NOT double*!
double *presult = &result;
ComputeSeriesPointer(x, n, presult);

The error message implies that the type of result is already double * . You don't need to use the & operator if the variable is already a pointer of the appropriate type. So you should do

ComputeSeriesPointer(x, n, result);

Either that, or you need to change the declaration of the result variable in the caller from double * to double .

It is likely that you are doing this:

double *myNewResult;
...
ComputeSeriesPointer(x, n, &myNewResult);

By doing this you are passing the address of a double* not double . You dont need double *myNewResult , just double myNewResult . Or if you need myNewResult to be a double* you can just pass it to the function like this:

ComputeSeriesPointer(x, n, myNewResult);

The function is declared like

void ComputeSeriesPointer(double, int, double*);

its third parameter has type double *

But you call the function like

ComputeSeriesPointer(x, n, &result);

where the third argument has type double **

You need to call the function like

ComputeSeriesPointer(x, n, result);

Or change the function declaration and correspondingly its definition such a way that the third parametr had type double **

void ComputeSeriesPointer(double, int, double **);

Passing a pointer into a function is passing by reference; the pointer is the "reference." (C++ muddied waters a little bit by also introducing a "reference" type which pretends it is not a pointer, but that's not relevant to the code example you've given.)

The & operator means "address of." It takes a thing and returns a pointer to that thing. So,

double x = 1; // this is a double
double *pointer_to_x = &x;
double **pointer_to_pointer_to_x = &pointer_to_x;

and so on.

We need to see a little bit more of your code calling ComputeSeriesPointer() to answer properly, but my guess is that you have:

double *result; // this kind of variable stores *the address of* a double-precision number. 
ComputeSeriesPointer( x, n, &result );

but you really want:

double result; // this kind of variable stores a double-precision number.
ComputeSeriesPointer( x, n, &result );

so that you are passing in the address that you want ComputeSeriesPointer() to write a result into.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM