# What is wrong with these pointers (C programming)?

include

#include

#define TAX_RATE 1.25

#define UTILITY_COST 300.0

#define INSURANCE_COST 550.0

void input(float*, float*, float*);

void calc(float, float, float, float, float, float, float, float);

float calcDplomo(float, float, float, float*, float*, float*, float*, float*);

void output(float, float, float, float, float, float, float, float);

int main(void)

{

float sellp;

float anir;

float loand;

float downp;

float amtl;

float mortg;

float propt;

float mtot;

input(&sellp, &anir, &loand);

calc(sellp, anir, loand, downp, amtl, mortg, propt, mtot);

output(sellp, anir, loand, downp, amtl, mortg, propt, mtot);

return 0;

}

void input(float* sellp, float* anir, float* loand)

{

printf(“\n\t\t Brian’s Mortgage Calculation Program\n”);

printf(“\nEnter the property’s selling price: \n”);

scanf(“%f”,sellp);

printf(“\nEnter the annual rate of interest: \n”);

scanf(“%f”,anir);

printf(“\nEnter the duration (years) of the loan: \n”);

scanf(“%f”,loand);

return;

}

void calc(float sellp, float anir, float loand, float downp, float amtl, float mortg, float propt, float mtot)

{

calcDplomo(sellp, anir, loand, &downp, &amtl, &mortg, &propt, &mtot);

return;

}

float calcDplomo(float sellp, float anir, float loand, float* downp, float* amtl, float* mortg, float* propt, float* mtot)

{

float cdown = .2;

float mins;

*propt = sellp * (TAX_RATE/100)/12;

mins = INSURANCE_COST/12;

*mtot = *mortg + UTILITY_COST + *propt + mins;

*downp = sellp * cdown;

*amtl = sellp – *downp;

*mortg = *amtl * anir/12*(1+anir/12) * loand *12/(1+anir/12) * loand *12-1;

return mins;

}

void output(float sellp, float anir, float loand, float downp, float amtl, float mortg, float propt, float mtot)

{

printf(“\nMONTHLY COST OF HOUSE\n”);

printf(“\nSELLING PRICE $ %9.2f”,sellp);

printf(“\nDOWN PAYMENT %9.2f”,downp);

printf(“\nAMOUNT OF LOAN %9.2f”,amtl);

printf(“\nINTEREST RATE %9.1f%%”,anir);

printf(“\nTAX RATE %9.1f%%”,TAX_RATE);

printf(“\nDURATION OF LOAN (YEARS) %9.0f\n”,loand);

printf(“\nMONTHLY PAYMENT\n”);

printf(“\nMORTGAGE %9.2f”,mortg);

printf(“\nUTILITIES %9.2f”,UTILITY_COST);

printf(“\nPROPERTY TAXES %9.2f”,propt);

printf(“\nUTILITIES %9.2f\n”,INSURANCE_COST);

printf(” __________”);

printf(” $ %9.2f\n”,mtot);

return;

}

Trial run:

Brian’s Mortgage Calculation Program

Enter the property’s selling price:

600000

Enter the annual rate of interest:

2.2

Enter the duration (years) of the loan:

30

MONTHLY COST OF HOUSE

SELLING PRICE $ 600000.00

DOWN PAYMENT -107374176.00

AMOUNT OF LOAN -107374176.00

INTEREST RATE 2.2%

TAX RATE 1.3%

DURATION OF LOAN (YEARS) 30

MONTHLY PAYMENT

MORTGAGE -107374176.00

UTILITIES 300.00

PROPERTY TAXES -107374176.00

UTILITIES 550.00

__________

$ -107374176.00

Press any key to continue

I know it has to do with the function call in void calc calcDplomo(sellp, anir, loand, &downp, &amtl, &mortg, &propt, &mtot); But how do I fix it?

At each calculation, add a printf() to display all of the variables that go into the calculation. That should help you find where your assumptions are wrong.

You asked this last week and got some good advice from another answerer…but didn’t seem to follow it. Your calc() function can’t possibly store any values because all of its arguments are values, not pointers. Every argument that a function needs to change has to be declared and handled as a pointer, not a float or double or int or other value.

I also notice that calcDplomo() uses *mortg before it has a value. That will cause problems after you fix everything else.

Suggestion. Use a prefix like p_mortg on argument names that are pointers, so you know when you are coding that they are pointers and need to be dereferenced to get a value. Example:

void calc(float sellp, float anir, float loand, float *p_downp, float *p_amtl, float *p_mortg, float *p_propt, float *p_mtot)

{

calcDplomo(sellp, anir, loand, p_downp, p_amtl, p_mortg, p_propt, p_mtot);

}

And call with:

calc(sellp, anir, loand, &downp, &amtl, &mortg, &propt, &mtot);

Also, consider making the prototype an exact copy of the function header, with argument names and all. When your programs get large enough to split into multiple files, you will find yourself using the prototype for reminding yourself what order to code the arguments. Having the argument names is a big help. Example:

void input(float* p_sellp, float* p_anir, float* p_loand); …. instead of:

void input(float*, float*, float*);

Finally, why does calcDplomo return a value when nobody looks at it? Shouldn’t it be a void function?