I'm trying to find the product of two numbers using a function which receives the values of two variables from main() and the address of the variable prd where the product will be stored.

I can't get this compiling because i get the following errors from Visual Studio C++ 2010 Express Edition:

f_p.c(13): error C2440: 'function' : cannot convert from 'float *' to 'float'
f_p.c(13): warning C4024: 'product' : different types for formal and actual parameter 3
f_p.c(13): warning C4028: formal parameter 3 different from declaration

Any idea where the problem might be?
I would appreciate a short explanation of what I'm doing wrong.

The code is the following:

#include <stdio.h>
#include <stdlib.h>

void product (float, int, float);

  main() {
	 float x, prd;
	 int y;

	 printf ("Give 2 Numbers; the fist float and the second int");
		scanf_s("%f %d", &x, &y);
	 
	 product (x, y, &prd);
	 
	 printf ("\nThe product is: %f\n\n", prd);

	 system("pause");
 }

 void product (float x, int y, float *prd){

	 *prd = x * y;
 }

You define your function header like

void product (float x, int y, float *prd)

and

void product (float, int, float)

Which are different.

It solved the problem but i have used the same type of declaration in another program and it worked just fine.
It was a swap program which swapped the values of two variables by passing the variables addresses from main to the function. The program worked as a charm.
Also isn't that the declaration before main of the function is supposed to tell the compiler that a function below main has to be linked and there should be reserved three places in memory for three variables. The names here are not important if I'm not mistaken.

This is very strange, I don't get it.

Thanks anyway :)

endritius> Also isn't that the declaration before main of the function is supposed to tell the compiler that a function below main has to be linked and there should be reserved three places in memory for three variables.
Not exactly. A function prototype is a concept "borrowed" from other language to help catching errors. It is a signature of what's expected to find once it gets to the function definition. Nothing about reserving memory, nothing about below main.

endritius> The names here are not important if I'm not mistaken.
Identifier names are not necessary in a prototype, however it is a good practice since it helps the programmer. Nevertheless, gerard4143 did point to you something different.
If

void product (float x, int y, float *prd)

then

void product (float, int, float *);

Your were providing the wrong type to the prototype, by just missing that (*)

Edited 6 Years Ago by Aia: n/a

This question has already been answered. Start a new discussion instead.