Function call by reference

I would just like to push in the right direction here with my homework. Here is the question:

(1) Write a function C called an input that returns void, this function asks the user to enter two integers, followed by a double precision value. This function reads these values ​​from the keyboard and finds the product of two integers entered. The function uses a link call to report values; three values ​​are considered and the product is calculated back to the main program. Then the main program prints three values ​​are counted and the product is calculated. Provide test results for 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 actually refer to variables with pointers, it just doesn’t work for me.

Any help would be great!

+4
source share
5 answers

You add to pro , but it is not initialized, you do not pass values ​​other than pro . You save the values ​​in the addresses of the passed variables. In this case, you need to specify dereferencing pointers to get / get the value, *i and use your passed addresses directly in your method - then you do not need to specify their address again.

This works - I replaced double with float ...:

 #include <stdio.h> #include <stdlib.h> void input(int *day, int *month, float *k, float *pro); int main(void){ int i,j; float k, pro; i = j = k = pro = 0; input(&i, &j, &k, &pro); printf("%f\n", pro); printf("%d : %d : %f\n", i,j,k); return 0; } void input(int *i, int *j, float *k, float *pro){ scanf("%d", i); scanf("%d", j); scanf("%f", k); printf("%d - %d - %f\n", *i,*j,*k); *pro += (*i * *j * *k); } 

Conclusion:

 1 2 3.5 1 - 2 - 3.500000 7.000000 1 : 2 : 3.500000 
+3
source

You are almost there, but instead of creating new variables x , y and z use the pointers you passed in:

 scanf("%d", i); scanf("%d", j); scanf("%f", k); *pro += ((*i) * (*j) * (*k)); 
+2
source

When reading numbers in the input function, you can use the iptr , jptr , kptr and proptr to read the values ​​directly in the variables i , j and k declared in the main function as:

 void input(int *iptr, int *jptr, double *kptr, double *proptr){ scanf("%d", iptr); // read directly into i using pointer to i. scanf("%d", jptr); scanf("%f", kptr); *proptr = ( (*iptr) * (*jptr) ); // compute product and assign to pro. } 
+1
source
 *pro += (x * y * z); 

It will break terribly. You add the product to any garbage that used to be in the pro. You want to remove + , i.e.:

 *pro = (x * y * z); 
+1
source

What your program does not do is set the input values ​​for i, j and k.

Instead of using x, y, and z, use options instead.

0
source

Source: https://habr.com/ru/post/1304735/


All Articles