# Thread: Pointer help

1. ## 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. 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.

3. Yes, those ampersands don't belong there. It works now. Thanks

Popular pages Recent additions