Thread: Help on pointer math please =)

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    Help on pointer math please =)

    Hey guys I am getting an error that I just cant figure out in this code:

    http://http://pastebin.com/9anaBP6f

    the error is:
    change.c:34: error: pointer value used where a floating point value was expected
    change.c:36: error: pointer value used where a floating point value was expected
    change.c:38: error: pointer value used where a floating point value was expected

    I'm just now learning how to use pointers so i apologize if I am completely off on syntax =/

    thanks for any sort of help =)

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    your problem is this
    Code:
     amount = amount - 0.25 * (double)quarters;
    change it to
    Code:
     amount = amount - 0.25 * ( double( *quarters) );
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    sorry I didnt realize the link didnt work...
    heres the code

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void coin_change(double amount, int *quarters, int *dimes, int *nickels, int *pennies);
    
    int
    main()
    {
            double price, pay, coins = 0;
            int dollars, *quarters, *dimes, *nickels, *pennies;
    
            printf("Welcome to the COINCOUNTER >9000!\n");
    
            printf("Input the amount due (0 to quit):  ");
            scanf("%lf", &price);
            printf("Input the amount paid:  ");
            scanf("%lf", &pay);
    
            printf("Your change is: \n");
    
            dollars = ceil(pay - price);
            printf("dollars: %d\n", dollars);
    
            coins = pay - price - dollars;
    
            coin_change(coins, quarters, dimes, nickels, pennies);
    
            return (0);
    }
    
    void coin_change(double amount, int *quarters, int *dimes, int *nickels, int *pennies)
    {
            *quarters = amount/0.25;
            amount = amount - 0.25 * (double)quarters;
            *dimes = amount/0.1;
            amount = amount - 0.1 * (double)dimes;
            *nickels = amount/0.05;
            amount = amount - 0.05 * (double)nickels;
            *pennies = ceil(amount/0.01);
    
            printf("quarters: %d\ndimes: %d\nnickels: %d\npennies: %d\n", *quarters, *dimes, *nickels, *pennies);
    }

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    segmentation fault

    ok, fixed that problem
    now I'm getting a segmentation fault when I call the function coin_change

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    because you have not allocated memory for them. it's better you just declare them to be int instead of pointer to int, then pass the address to the function.
    Code:
    int quarter; 
    coin_change( something, &quarter, something);
    "All that we see or seem
    Is but a dream within a dream." - Poe

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    Awesome! =)
    thanks so much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers
    By MrMatt027 in forum C++ Programming
    Replies: 14
    Last Post: 12-10-2010, 04:32 PM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM