Thread: Using pointers, is it bad?

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    21

    Using pointers, is it bad?

    Hi,
    I wrote this very simple C console app. It could've been written simpler way; however, I used pointers instead of normal variable to manipulate data (since I'm enjoying them atm). I was wondering if there was any memory leaks in my code. As far as I understand, I didn't use any heap allocated memory, so there shouldn't be any memory leaks. Since, I'm new I might be wrong. Any suggestions on improving the code or coding in general would be really appreciated. Thanks.
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    
    
    int getCommand()
    {
        int commandValue;
        bool done = false;
        do
        {
            printf("Enter command: ");
            scanf(" %d", &commandValue);
            if (commandValue >= 0 && commandValue < 5) done = true;
        } while (!done);
        return commandValue;
    }
    
    
    void displayMainMenu()
    {
        printf("*** ACME checkbook-balancing program ***\nCommands: 0=clear, 1=credit, 2=debit, 3=balance, 4=exit\n\n");
    }
    
    
    void exitApp()
    {
        exit(0);
    }
    
    
    void addBalance(float* balance, float amount)
    {
        *balance += amount;
    }
    
    
    void withdrawBalance(float* balance, float amount)
    {
        *balance -= amount;
    }
    
    
    void getAmount(char* message, float* amount)
    {
        printf("Enter amount of %s: ", message);
        scanf("%f", amount);
    }
    void printBalance(float* balance)
    {
        printf("Current balance: $%.2f\n", *balance);
    }
    
    
    void clearBalance(float* balance)
    {
        *balance = 0.0f;
    }
    
    
    int main()
    {
        displayMainMenu();
        bool continueApp = true;
        float balance, amount;
        while (continueApp)
        {
            int option = getCommand();
            switch (option)
            {
            case 0:
                clearBalance(&balance);
                break;
            case 1:
                getAmount("credit", &amount);
                addBalance(&balance, amount);
                break;
            case 2:
                getAmount("debit", &amount);
                withdrawBalance(&balance, amount);
                break;
            case 3:
                printBalance(&balance);
                break;
            case 4:
                continueApp = false;
                break;
            }
        }
        exitApp();
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Well after a quick glance the only thing that really stands out is that your display function probably shouldn't have the pointer parameter since the value should never change. Also your withdraw and add functions could probably be one function, just pass a negative value into the function for the withdraw.

    Lastly your exitApp() function is really not necessary, when the program encounters the ending brace of main() it will "exit' (actually return) to the operating system.

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    21
    good points. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-29-2015, 01:15 PM
  2. Replies: 43
    Last Post: 05-23-2013, 03:01 PM
  3. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  4. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  5. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM

Tags for this Thread