Complementary task for topic: 6

M Nemeth · 2023-08-29 15:21:04.627218'

Pointers: Pass to function by address

Pointers: Pass to function by address

Write a program, that doubles a complex number. The doubling should be realized in a separate function

Hint: The main problem is that you cannot return with two values, so you cannot return with imaginary and real part as well. One solution is shown with structs, the other is to pass the variable of the results by address, so the function can modify its content. Reminder: arguments and return vales are copies! Anything declared in the function will be deleted when the function returns (stack behavior)

Solution
#include 


void double_complex(double re, double im, double* RES_re, double* RES_im){
    *RES_re=2*re;
    *RES_im=2*im;
    return;


}

int main()
{
    double re=1.0, im=2.0; //to be doubled
    double doubled_re, doubled_im; //results
    double_complex(re,im,&doubled_re,&doubled_im);
    printf("The double of %lf + %lf i is %lf + %lf i",re,im,doubled_re,doubled_im);
    return 0;
}



Explanation

< < previous    next > >