简体   繁体   中英

How can you pass an int variable into a function with float parameters in C++ without getting compile errors?

My professor gave us two examples of code in which both pass the int values, x & y, into a function with float parameters as examples of how to pass a different type of variable into a function by reference. I've ran them both, and neither work, unless of course I change int x, y; to float x, y; . The programs are written in C.

In his notes, following the 2nd program, he makes his point regarding the comparison of the 2:

"It (both programs) prints out: normalized vector = ( .12, .16 ) This code is nearly identical to the code of the previous example. The difference is in the way the parameters are declared in the normalizeVector function. By using the "&" operator, the normalizeVector function has access to the variables being passed to it, not just access to the values of those variables, access to the original location. This is functionally the same as having the address of those variables, but the syntax is nicer for those not used to using C pointers. Since the "&" is used, all of the work de-referencing the variables using the "*", as in the previous example, can be avoided."

While I am not questioning the rule or technique, I can't run them and am dying to know what stupid little mistake I am making or what crazy (and probably very simple) detail I am missing. I have done much more than just copy and paste them. Not to mention, when I used int float to make them work, I had to remove the sqrt before (xWork*xWork + yWork*yWork) in order to get the correct answers. (And by the way, he is a great professor.) Should these programs work? And how do I pass int into float parameters by reference, if they don't?

Program 1

main()
{
    int x, y;   
    x = 3;
    y = 4; 

    normalizeVector(&x, &y);    
    printf(“normalized vector = ( %f, %f ) \n”, x, y);
}  

void normalizeVector(float *x, float *y)
{
    float magnitude;
    float xWork, yWork;
    xWork = *x;
    yWork = *y; 

    magnitude = sqrt(xWork*xWork + yWork*yWork);
    *x = xWork/magnitude;
    *y = yWork/magnitude;
}

Program 2

main()
{
    int x, y;    
    x = 3;
    y = 4;

    normalizeVector(x, y);
    printf(“normalized vector = ( %f, %f ) \n”, x, y);
}  

void normalizeVector(float &x, float &y)
{
    float magnitude;
    float xWork, yWork;
    xWork = x;
    yWork = y;

    magnitude = sqrt(xWork*xWork + yWork*yWork);
    x = xWork/magnitude;
    y = yWork/magnitude;
}

Either this code was copied wrongly or the professor is confused.

C and C++ both have some limited rules allowing built in type numeric arguments to functions which are passed by value to be implicitly converted to the needed type, if they are compatible according to a set of parameters explained in the spec (which I wont go into here due to their complexity).

This does not apply to parameters passed by pointer or reference, you need to pass in a pointer to the exact type the function needs if it is passed by reference (there are a few implementation defined tricks involving reinterpret casts but this is unimportant for this case).

So yes, you are correct, the only way this code could possibly work is if you change the variables to the correct type or change the type the function requires.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM