David Peterson Harvey David Peterson Harvey - 3 months ago 73
C Question

C Student Assignment, ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’

I'm working on an assignment and I'm getting this warning:

C4_4_44.c:173:2: warning: format ‘%f’ expects argument of type ‘float *’,
but argument 2 has type ‘double *’ [-Wformat]


The variabled is declared in main as:

double carpetCost;


I'm calling the function as:

getData(&length, &width, &discount, &carpetCost);


And here's the function:

void getData(int *length, int *width, int *discount, double *carpetCost)

{

// get length and width of room, discount % and carpetCost as input

printf("Length of room (feet)? ");

scanf("%d", length);

printf("Width of room (feet)? ");

scanf("%d", width);

printf("Customer discount (percent)? ");

scanf("%d", discount);

printf("Cost per square foot (xxx.xx)? ");

scanf("%f", carpetCost);

return;

} // end getData


This is driving me crazy because the book says that you don't use the & in

scanf("%f", carpetCost);


when accessing it from a function where you passed it be reference.

Any ideas what I'm doing wrong here?

Answer

Change

scanf("%f", carpetCost);

with

scanf("%lf", carpetCost);

%f conversion specification is used for a float * argument, you need %lf for double * argument.