C 指针。这种编程风格好吗?

C 指针。这种编程风格好吗?,c,pointers,C,Pointers,我有一个任务,我应该编写一个简单的出纳员机器,它可以取出10美元的钞票,然后将这些钞票兑换成100美元、50美元、20美元和10美元的钞票。应退还最少金额的票据 作业主要关注指针,我开始怀疑我所做的是否是好的编程风格,因为我相信我把指针指向了指针(例如,&*balance) 这是一个好方法还是我应该重写它 这是我的mymain: int main(void) { int amount = 0; // Amount of 10 dollar bills int balance =

我有一个任务,我应该编写一个简单的出纳员机器,它可以取出10美元的钞票,然后将这些钞票兑换成100美元、50美元、20美元和10美元的钞票。应退还最少金额的票据

作业主要关注指针,我开始怀疑我所做的是否是好的编程风格,因为我相信我把指针指向了指针(例如,
&*balance

这是一个好方法还是我应该重写它

这是我的my
main

int main(void) {
    int amount = 0; // Amount of 10 dollar bills
    int balance = 0; // Total balance (in dollars)
    int hundred = 0, fifty = 0, twenty = 0, ten = 0; // Amount of bills calculated

    // Set number of 10 dollar bills
    printf("Enter amount of 10 dollar bills: ");
    scanf("%d", &amount);

    // Set balance to user's 10 dollar bills
    valueOfBills(&balance, &amount, BILL_VALUE_TEN);

    // Calculate amount of bills for the different types
    amountOfBills(&balance, &hundred, &fifty, &twenty, &ten);
}
在我的
main
中,我调用
amountOfBills()

void amountOfBills(int *balance, int *hundred, int *fifty, int *twenty, int *ten) {
    /* To end up with the least amount of bills, we will start with bills with the largest value */

    // Calculate amount of 100 dollar bills
    amountOfBillsForBillValue(&*hundred, &*balance, BILL_VALUE_HUNDRED);

    // Calculate amount of 50 dollar bills
    amountOfBillsForBillValue(&*fifty, &*balance, BILL_VALUE_FIFTY);

    // Calculate amount of 20 dollar bills
    amountOfBillsForBillValue(&*twenty, &*balance, BILL_VALUE_TWENTY);

    // Calculate amount of 10 dollar bills
    amountOfBillsForBillValue(&*ten, &*balance, BILL_VALUE_TEN);
}
void amountOfBillsForBillValue(int *storeAmount, int *balance, int billValue) {
    /* We don't want to do anything if the balance is 0
       therefore we will just return to avoid any calculations */
    if (balance == 0) return;

    // Calculate amount
    *storeAmount = *balance / billValue;

    // Set balance
    *balance %= billValue;
}
amontofbills()
中,我调用
amontofbillsforbillvalue()
计算要返回的特定票据(例如100美元票据)的数量:

void amountOfBills(int *balance, int *hundred, int *fifty, int *twenty, int *ten) {
    /* To end up with the least amount of bills, we will start with bills with the largest value */

    // Calculate amount of 100 dollar bills
    amountOfBillsForBillValue(&*hundred, &*balance, BILL_VALUE_HUNDRED);

    // Calculate amount of 50 dollar bills
    amountOfBillsForBillValue(&*fifty, &*balance, BILL_VALUE_FIFTY);

    // Calculate amount of 20 dollar bills
    amountOfBillsForBillValue(&*twenty, &*balance, BILL_VALUE_TWENTY);

    // Calculate amount of 10 dollar bills
    amountOfBillsForBillValue(&*ten, &*balance, BILL_VALUE_TEN);
}
void amountOfBillsForBillValue(int *storeAmount, int *balance, int billValue) {
    /* We don't want to do anything if the balance is 0
       therefore we will just return to avoid any calculations */
    if (balance == 0) return;

    // Calculate amount
    *storeAmount = *balance / billValue;

    // Set balance
    *balance %= billValue;
}

amountOfBills()
中,我使用
和*balance
。我应该用另一种方式还是可以这样做?

&*
相互抵消。只要
balance
就可以为您提供与
和*balance
相同的服务,并且只需少两个字符。另一方面,如果您只想记住它是poiner,那么使用
&*
不会造成任何伤害,编译器将非常清楚它是一个noop

然而,还有一件事绝对不是好的风格。为各种类型的注释使用显式参数的方式。如果当你完成这项工作时,有人来告诉你“我们已经增加了500美元的钞票,我们不会费心填20美元的钞票”,你将不得不修改所有内容。你应该概括一下。具有值数组和计数数组

我相信我将指针指向指针(例如,
&*balance

你不是。您正在解引用指针,然后获取结果的地址


换句话说,写
和*balance
与写
balance
是一样的。我建议您使用后者。

如果我是您,我会将这些参数分组到结构中:

typedef struct
{
  int balance;
  int hundred;
  int fifty;
  int twenty;
  int ten;
} balance_t;


这样,只向函数传递一个指针,另一个方法也是如此,避免函数有许多参数

&*
的危害是对其他可怜的程序员来说,他们会浪费时间想知道你为什么这么做。非常感谢。你是对的,修改文件可能会一团糟。如果我能把它改写得更通俗一些,我会试试的。@blastfull:True。另一方面,这是一种怪癖,几乎像
&(x[y])
x+y
相比。人们必须学会忽略这些差异。缺乏抽象是一个严重的问题,不必要的运算符不是。在
amountOfBillsForBillValue
中,如果要检查
balance
您需要取消引用指针以检查它所指向的值,因此要检查
balance
是否为0,您应该执行
if(*balance==0)
正如您在其他计算中所做的那样。这可能更适合现在我得到它。非常感谢。这很有帮助,好主意。我来看看这个。看起来很聪明。