Call by Reference Function in C
Posted
by Chad
on Stack Overflow
See other posts from Stack Overflow
or by Chad
Published on 2010-03-22T05:50:56Z
Indexed on
2010/03/22
6:01 UTC
Read the original article
Hit count: 391
Hello everyone,
I would just like a push in the right direction here with my homework assignment. Here is the question:
(1) Write a C function called input which returns void, this function prompts the user for input of two integers followed by a double precision value. This function reads these values from the keyboard and finds the product of the two integers entered. The function uses call by reference to communicate the values of the three values read and the product calculated back to the main program. The main program then prints the three values read and the product calculated. Provide test results for the input: 3 5 23.5. Do not use arrays or global variables in your program.
And here is my code:
#include <stdio.h>
#include <stdlib.h>
void input(int *day, int *month, double *k, double *pro);
int main(void){
int i,j;
double k, pro;
input(&i, &j, &k, &pro);
printf("%f\n", pro);
return 0;
}
void input(int *i, int *j, double *k, double *pro){
int x,y;
double z;
double product;
scanf("%d", &x);
scanf("%d", &y);
scanf("%f", &z);
*pro += (x * y * z);
}
I can't figure out how to reference the variables with pointers really, it is just not working out for me.
Any help would be great!
© Stack Overflow or respective owner