c - 将新数据读取到函数内的指针参数中?

标签 c function pointers parameters

我正在创建一个程序,允许用户输入行星数据。在这个函数中,我应该有3个指针参数,这样我就可以将函数外部声明的变量的地址传递给指针,然后使用scanf将数据读入这些地址(以指针参数的形式)。

程序可以编译,但是当我输入新的行星数据时程序崩溃了:(

void new_planet_data(double *temp_mass, double *temp_radius,    double * temp_density)
{


    printf("Enter the planet's mass (earth = 5.9736e24): ");
    scanf("%lf",&temp_mass);

    printf("Enter the planet's radius (earth = 6.37101e6): ");
    scanf("%lf",&temp_radius);

    printf("Enter the air density (earth = 1.2):");
    scanf("%lf",&temp_density);

    planet_mass = *temp_mass;

    planet_radius = *temp_radius;

    planet_density = *temp_density;


}

最佳答案

temp_x 变量已经是指针,因此不需要在 scanf 中使用 & 运算符。

关于c - 将新数据读取到函数内的指针参数中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8089077/

相关文章:

c++ - 用于创建业余操作系统的最受尊敬的语言和免费编译器?

c - 最好使用scanf或fgets?

c++ - 将变量分配给数组的函数

javascript - 在 javascript 中复制函数

r - 计算 r 中数据函数的导数

C CRC16 的 Java 等价物

c - Visual Studio代码调试C请求超时

c++ - boost 侵入式指针

c++ - 访问冲突读取位置0xcdcdcdcd - VS 2010 win7

将 char arr[64] 复制到 char arr[] 会导致段错误吗?