指向int指针幂的c++浮点指针

c++ floating pointer to the power of int pointer

本文关键字:指针 c++ int 指向      更新时间:2023-10-16

我有一个方法,它接受一个float指针和一个int指针,然后执行pow(the float, the int)并返回值。我犯了一个巨大的错误,我似乎不明白它告诉我什么是错误的。

#include <cmath>
#include <iostream>
using namespace std;
float method3(float *f, int *i); //initialize pointer method
float flt; //init variable
int nt; //init variable
int main() {
    method3(*flt, *nt); //run method 3, which will do the same math, but with pointers instead of value or reference
    cout << flt; //print it out
    return 0;
}
float method3(float *f, int *i) { //method 3, get float and int by pointers
    return pow(f, i); //f to power of i back to original flt variable
}

请告诉我我做错了什么?

您不正确地取消引用指针。

你应该打电话给

pow(*f,*i);

method3(&flt,&nt);

method3返回void,因此不能在其中写入return pow(f, i);