Thread: Pointer help

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    3

    Pointer help

    Code:
    #include <stdio.h>
    
    double calculate_tp(int quantity, double up_dollars, double up_pounds, double *tpa, double *tpb);
    
    int main(void)
    {
    	char again;
    	double up_dollars = 30.0, up_pounds = 15.0; /*Unit price*/
    	int quantity;
    	double tp_dollars,tp_pounds; /* Total price */
    	
    	for (again = 'y'; again != 'n'; scanf(" %c",&again)){
    		printf("Unit cost is %.2lf dollars or %.2lf pounds \n",up_dollars,up_pounds);
    		printf("Enter quantity>");
    		scanf(" %d",&quantity);
    		printf("Quantity %d \n \n",quantity);
    		calculate_tp(quantity,up_dollars,up_pounds,&tp_dollars,&tp_pounds);
    		printf("Total in dollars %.2lf \n",&tp_dollars);
    		printf("Total in pounds %.2lf \n \n",&tp_pounds);
    		printf("Run program again?");
    		
    	}
    	return(0);
    	
    }
    
    double calculate_tp(int quantity, double up_dollars, double up_pounds, double *tpa, double *tpb)
    {
    	*tpa = quantity * up_dollars;
    	printf("Test print  total dollars %.2lf \n",*tpa);
    	*tpb = quantity * up_pounds;
    	printf("Test print total pounds %.2lf \n",*tpb);
    	
    }
    The test printf's work. However the printfs in the main for the totals display zero always. Any ideas as to why the value doesnt seem to pass to tp_dollars and tp_pounds?

    The program doesnt really make much sense but I'm just trying to learn using pointers with functions.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    printf("Total in dollars %.2lf \n",&tp_dollars);
    printf("Total in pounds %.2lf \n \n",&tp_pounds);

    These two lines are specifying to print the address of both variables (that & sign, ampersand, means you're working with the address of a variable).

    The rest seems to be okay (but I glanced rather quickly), so try removing those ampersand signs in order to print the actual values of the variables.

    EDIT:
    Also, the reason you're getting zeroes is probably from that fact that it's looking for a double (%.2f) and you're passing it an integer (the address), so it doesn't know how to handle it.
    Last edited by Epo; 05-18-2005 at 08:03 PM.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    3
    Yes, those ampersands don't belong there. It works now. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 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