【问题标题】:Read new data into pointer parameters inside a function?将新数据读入函数内的指针参数?
【发布时间】:2011-11-11 02:44:45
【问题描述】:

我正在创建一个允许用户输入行星数据的程序。在这个函数中,我应该有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;


}

【问题讨论】:

  • 那么您是否尝试过在调试器中运行您的程序?

标签: c function pointers parameters


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-15
    • 2016-03-14
    • 1970-01-01
    • 2019-06-12
    • 2018-08-25
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    相关资源
    最近更新 更多