我是一名 C++ 程序员,试图掌握 C 语言语法,但我不明白为什么在此函数中使用指针时会得到垃圾值。
#include<stdio.h>
#include <math.h>
void separate(double, double *dollar, double *quarter, double *dime, double *nickle, double *penny);
int
main(void)
{
double amount = 38.39, change, paid = 40.0, dollar, quarter, dime, nickle, penny;
change = paid - amount;
separate(change, &dollar, &quarter, &dime, &nickle, &penny);
printf("Your total change is: $%d\n", change);
printf("Dollars: %d\n", dollar);
printf("Quarters: %d\n", quarter);
printf("Dimes: %d\n", dime);
printf("Nickles: %d\n", nickle);
printf("Pennies: %d\n", penny);
getchar();
return (0);
}
void separate(double change, double *dollar, double *quarter, double *dime, double *nickle, double *penny)
{
double coins;
coins = change - floor(change);
*dollar = floor(change);
*quarter = coins / 25;
coins = coins - (*quarter * 25);
*dime = coins / 10;
coins = coins - (*dime * 10);
*nickle = coins / 5;
*penny = coins - (*nickle * 5);
}