You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言指针解引用问题:购票程序指针使用错误排查与修正

C语言指针作业代码错误排查与修正

需求说明

需编写C语言程序,实现以下功能:

  • 询问用户可支配金额、欲购买的成人票及儿童票数量
  • 程序必须包含main()和PurchaseTickets()两个函数
  • 剩余现金变量必须通过指针实现
  • 若用户资金不足,程序返回0

以下是提交的代码,存在指针使用相关错误,需排查并修正:

#include <stdio.h>

#define ADULT_TICKET_PRICE 69.00                                                    //define constants
#define CHILD_TICKET_PRICE 35.00

int PurchaseTickets(double *pRemainingCash, int adultTickets, int childTickets);    //declare function PurchaseTickets()

int main(void)
{
    double funds;
    int childTickets;
    int adultTickets;

    printf("How much money do you have to purchase tickets?\n");
    if(scanf("%lf", &funds) != 1)                        //check for errors in user input
    {
        printf("Invalid input, exiting program.\n");
        return 1;                                       //will exit program if invalid characters are entered
    }

    printf("How many child tickets would you like to purchase?\n");
    if(scanf("%d", &childTickets) != 1)                 //check for errors in user input
    {
        printf("Invalid input, exiting program.\n");
        return 1;                                       //will exit program if invalid characters are entered
    }

    printf("How many adult tickets would you like to purchase?\n");
    if(scanf("%d", &adultTickets) != 1)                 //check for errors in user input
    {
        printf("Invalid input, exiting program.\n");
        return 1;                                       //will exit program if invalid characters are entered
    }

    double *pfunds = &funds;

    PurchaseTickets(pfunds, adultTickets, childTickets);

    return 0;
}

/*****************************************************************************************
 *
 *  Function:  PurchaseTickets()
 *
 *  Parameters:
 *
 *      pRemainingCash - points to a variable containing the amt of cash from user
 *      adultTickets - specifies the number of adult tickets the user wants to buy
 *      childTickets - specifies the number of child tickets the user wants to buy
 *
 *  Description: 
 *
 *      The function will determind if the user has enough money to purchase the
 *      specified number of tickets. If they do, the function deducts the proper funds
 *      from their remaining cash and returns the total number of tickets purchased.
 *      if they do not the function returns 0.
 *
*****************************************************************************************/

int PurchaseTickets(double *pRemainingCash, int adultTickets, int childTickets)
{
    double adultCost = (adultTickets * ADULT_TICKET_PRICE);
    double childCost = (childTickets * CHILD_TICKET_PRICE);
    double totalCost = adultCost + childCost;

    int totalTickets = adultTickets + childTickets;

    double remainingCash = *pRemainingCash;
    pRemainingCash = &remainingCash;

    pRemainingCash = remainingCash - totalCost;


    if (*pRemainingCash >= totalCost)
    {
        printf("You have purchased %d tickets, and you have %.2f remaining.",totalTickets, pRemainingCash);
    }

    else
    {
        printf("You do not have enough money to purchase the tickets.");
        return 0;
    }

    return 1;
}

错误分析

  • 指针指向错误:在PurchaseTickets()中,将pRemainingCash指向局部变量remainingCash,导致后续修改无法同步到main()的funds变量,且局部变量销毁后指针悬空,引发未定义行为。
  • 指针赋值逻辑错误:pRemainingCash = remainingCash - totalCost; 试图将double类型的计算结果赋值给指针变量,类型完全不匹配,正确做法是修改指针指向的变量的值(使用*pRemainingCash)。
  • 判断条件逻辑错误:if (*pRemainingCash >= totalCost) 错误地用修改后的剩余金额判断,正确逻辑是先检查用户初始可支配金额是否足够支付总票价。
  • 输出格式错误:printf中直接使用pRemainingCash(指针变量)会输出内存地址而非金额,需改为*pRemainingCash。
  • 返回值不符合要求:函数成功时应返回总票数,但原代码返回1,不符合需求。

修正后的代码

#include <stdio.h>

#define ADULT_TICKET_PRICE 69.00
#define CHILD_TICKET_PRICE 35.00

int PurchaseTickets(double *pRemainingCash, int adultTickets, int childTickets);

int main(void)
{
    double funds;
    int childTickets;
    int adultTickets;

    printf("How much money do you have to purchase tickets?\n");
    if(scanf("%lf", &funds) != 1)
    {
        printf("Invalid input, exiting program.\n");
        return 1;
    }

    printf("How many child tickets would you like to purchase?\n");
    if(scanf("%d", &childTickets) != 1)
    {
        printf("Invalid input, exiting program.\n");
        return 1;
    }

    printf("How many adult tickets would you like to purchase?\n");
    if(scanf("%d", &adultTickets) != 1)
    {
        printf("Invalid input, exiting program.\n");
        return 1;
    }

    // 直接传funds的地址,无需额外定义指针变量
    int result = PurchaseTickets(&funds, adultTickets, childTickets);
    
    // 在main中展示最终剩余金额
    printf("\nFinal remaining cash: %.2f\n", funds);

    return result;
}

int PurchaseTickets(double *pRemainingCash, int adultTickets, int childTickets)
{
    double adultCost = adultTickets * ADULT_TICKET_PRICE;
    double childCost = childTickets * CHILD_TICKET_PRICE;
    double totalCost = adultCost + childCost;

    int totalTickets = adultTickets + childTickets;

    // 先判断初始金额是否足够支付总票价
    if (*pRemainingCash >= totalCost)
    {
        // 修改指针指向的变量,更新剩余金额
        *pRemainingCash -= totalCost;
        printf("You have purchased %d tickets, and you have %.2f remaining.", totalTickets, *pRemainingCash);
        // 返回总票数
        return totalTickets;
    }
    else
    {
        printf("You do not have enough money to purchase the tickets.");
        // 资金不足返回0
        return 0;
    }
}

修正说明

  1. 指针使用修正:不再将指针指向局部变量,直接通过*pRemainingCash修改main()中funds的值,确保剩余金额的修改同步到主函数。
  2. 逻辑判断修正:先检查用户初始可支配金额是否足够支付总票价,符合业务逻辑。
  3. 输出修正:使用*pRemainingCash输出剩余金额,而非指针地址。
  4. 返回值修正:成功购买时返回总票数,资金不足返回0,符合需求。
  5. 代码简化:main()中无需额外定义指针变量,直接传递&funds即可。

内容的提问来源于stack exchange,提问作者Leila Jones

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 13:47:49