开发者

Call by Reference Function

开发者 https://www.devze.com 2022-12-24 11:26 出处:网络
I would just like a push in the right direction here with my homework assignment. Here is the question:

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!


You adding to pro but that is not initialized, you are not passing values back apart from pro. You store values into the addresses of variables passed in. In that case you need to dereference pointers to access/retrieve value, *i, and in your method use the passed addresses directly - then you don't need to take address of them 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);
}

Output:

1
2
3.5
1 - 2 - 3.500000
7.000000
1 : 2 : 3.500000


You're almost there, but instead of making new variables x, y, and z, use the pointers you passed:

scanf("%d", i);
scanf("%d", j);
scanf("%f", k);

*pro += ((*i) * (*j) * (*k));


When reading the numbers in the input function you can make use of the pointers iptr, jptr, kptr and proptr to read the values directly into 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.
} 


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

This is going to break horribly. You're adding the product to whatever garbage happens to be in pro beforehand. You want to remove the +, i.e.:

*pro = (x * y * z);


What your program is not doing is setting the values of the input to i, j, and k.

Instead of using x,y and z, use the parameters instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消