C指针给随机值

C pointer giving random value

我自学 C 是为了好玩。我在网上查找了一些练习程序,发现其中有两个输入:一个是虚构产品的数量,其次是虚构客户给你的钱数。然后你 return 美元钞票的数量,并找零给客户。

这是我为解决这个问题而写的完整代码:

#include <stdio.h>
#include <stdlib.h>

float getDif(float c, float m)
{
    // First find the difference between
    // c & m (m - c = d)
    float dif = m - c;

    // Return the difference
    return dif;
}

void getChange(float dif, int* cash, float* change)
{
    // If difference is less than 0 we have a problem
    // because the customer gave us less money than
    // the cost of the product
    if(dif < 0)
    {
        fprintf(stderr, "The customer has not given enough money!...\n");
        exit(EXIT_FAILURE);
    }
    // Cash is seen as the difference without any
    // decimals. So if the difference is .45
    // the cash would be 13 since cash is, in my
    // mind paper money
    *cash = (int) dif;

    // Change is seen as the difference - cash
    // if the difference is .45 the change
    // would be .45
    *change = dif - *cash;
}

void getCash(int cash, int* oneHundred, int* fifty, int* twenty, int* ten,
                int* five, int* one)
{
    if(cash == 0)
    {
        printf("Wow close to exact change! - No need for cash...\n");
        // End the function there is no need to continue
        return;
    }

    // Since cash is > 0 we need to determine the bills to return
    // to the user
    if(cash >= 100)
    {
        while(cash >= 100)
        {
            cash = cash - 100;
            (void) ++*oneHundred;
        }
    }

    if(cash >= 50)
    {
        while(cash >= 50)
        {
            cash = cash - 50;
            (void) ++*fifty;
        }
    }

    if(cash >= 20)
    {
        while(cash >= 20)
        {
            cash = cash - 20;
            (void) ++*twenty;
        }
    }

    if(cash >= 10)
    {
        while(cash >= 10)
        {
            cash = cash - 10;
            (void) ++*ten;
        }
    }

    if(cash >= 5)
    {
        while(cash >= 5)
        {
            cash = cash - 5;
            (void) ++*five;
        }
    }

    if(cash >= 1)
    {
        while(cash >= 1)
        {
            cash = cash - 1;
            (void) ++*one;
        }
    }

    printf("After all loops cash = %d\n", cash);
}

void getCoins(float change, int* quarter, int* dime, int* nickel, int* penny)
{
    // To find the correct change we need to turn the
    // current format of variable change (0.57) into
    // a easier to work with 57.
    int tenChange = change * 100;

    if(tenChange >= 25)
    {
        while(tenChange >= 25)
        {
            tenChange = tenChange - 25;
            (void) ++*quarter;
        }
    }

    if(tenChange >= 10)
    {
        while(tenChange >= 10)
        {
            tenChange = tenChange - 10;
            (void) ++*dime;
        }
    }

    if(tenChange >= 5)
    {
        while(tenChange >= 5)
        {
            tenChange = tenChange - 5;
            (void) ++*nickel;
        }
    }

    if(tenChange >= 1)
    {
        while(tenChange >= 1)
        {
            tenChange = tenChange - 1;
            (void) ++*penny;
        }
    }

    printf("After all loops change = %d\n", tenChange);
}

int main(void)
{
    // Create variables for the various things we create
    float c, m, dif, change;
    int cash, oneHundred, fifty, twenty, ten, five, one, quarter, dime, nickel,
        penny;

    printf("Enter the exact amount of the items (18.37): ");
    // Obtain the cost
    scanf("%f", &c);

    printf("Enter the amount of money given by the customer: ");
    // Obtain the money from customer
    scanf("%f", &m);

    // Obtain the difference of the cost
    // And the money given by calling the
    // getDif() function
    dif = getDif(c,m);

    // Send the difference to the getChange()
    // function, as well as the pointers 
    // cash & change which will be used in the
    // function and returned back here
    getChange(dif, &cash, &change);

    // First send the cash variable to the getCash
    // function along with pointers for each bill
    // The function will calculate the number of bills
    // to give to the customer and return each
    getCash(cash, &oneHundred, &fifty, &twenty, &ten, &five, &one);

    // Print the number of bills to give to the customer
    printf("Give the customer %d Hundred doller bill(s)!\n", oneHundred);
    printf("Give the customer %d Fifty doller bill(s)!\n", fifty);
    printf("Give the customer %d Twenty doller bill(s)!\n", twenty);
    printf("Give the customer %d Ten doller bill(s)!\n", ten);
    printf("Give the customer %d Five doller bill(s)!\n", five);
    printf("Give the customer %d One doller bill(s)!\n", one);
    
    // Second send the change variable to the getCoins
    // function along with pointers for each type of
    // coin. The function will calculate the number of
    // coins to give to the customer and return each
    getCoins(change, &quarter, &dime, &nickel, &penny);

    // Print the number of coins to give to the customer
    printf("Give the customer %d Quarter(s)\n", quarter);
    printf("Give the customer %d Dime(s)\n", dime);
    printf("Give the customer %d Nickel(s)\n", nickel);
    printf("Give the customer %d Penny(s)\n", penny);

    printf("%d\n", cash);
    printf("%.2f\n", change);
    printf("$%.2f\n", dif);
    return 0;
}

它工作正常,直到我向客户打印出 Nickels 和 Pennies 的数量。它没有给我一个有意义的数字,而是给我一串随机数字。

例如,这是这些输入的输出:

Enter the exact amount of the items (18.37): 123.32

Enter the amount of money given by the customer: 124.00

Wow close to exact change! - No need for cash...
Give the customer 0 Hundred doller bill(s)!
...
Give the customer 0 One doller bill(s)!
After all loops change = 0
Give the customer 2 Quarter(s)
Give the customer 1 Dime(s)
Give the customer 32768 Nickel(s)
Give the customer 1596836443 Penny(s)
0
0.68
[=11=].68

现在如果程序运行正常,它应该说:

但我得到的是那些随机 (?) 值。这可能是我没有清除指针的问题吗?我从来没有使用过使用它们的语言,而且我没有学到足够的知识来正确使用它们。

我试图将所有现金变量(例如 oneHundred)设置为 NULL,但它给我这个编译器错误:

change.c:204:14: warning: incompatible pointer to integer conversion assigning
      to 'int' from 'void *' [-Wint-conversion]
        oneHundred      = NULL;
                        ^ ~~~~

任何帮助都会很棒!我才刚刚开始学习这门语言,我希望这个问题能让我对将来如何写出更好的 C 有所了解!谢谢!!

你不需要将它们设置为 NULL 因为它们不是指针,像这样将它们初始化为 0

cash = oneHundred = fifty = twenty = ten = five = one = quarter = dime = nickel = penny = 0;

NULL 用于将指针初始化为一个特殊值,因此您可以对其进行测试以检查指针是否指向某处。