简体   繁体   中英

how to pass element of array as argument to function for it to modify, in ANSI C program?

I understand how to pass an array to a function and let that function update the value of the array. I'm having a tough time understanding how to pass an ELEMENT of an array to a function and have that function update that element. My guess is as below, but produces the following from gcc: "warning: conflicting types for 'CDivide'" .

The main() function defines the arrays in question, and passes them (as reference) to fn_v1p0 as complex_real and complex_imag , where they're declared as pointers in the function definition for fn_v1p0 . The main() includes prototypes for these functions, as follows.

void fn_v1p0(double *, double *, char *, double *, unsigned int, double *);
void CDivide(double *, double *, double, double, double, double);

However, the main() sits in one file and the other two functions sit in a different file. Only the main file includes these prototypes.

I need to perform complex division (not supported in ANSI C), which is the purpose of function CDivide . There's more to the fn_v1p0 function, but I reduced it to only what's necessary to show for this question.

I've treated complex_real[ii] as simply a double value, hoping that if I put an & in front of it then CDivide could be written as shown. But my guess isn't working. Not sure what's the best way to tackle this.

void fn_v1p0(
    double *complex_real, double *complex_imag, 
    char *selected_form,    
    double *freq,          
    unsigned int len_freq, 
    double *Hparams)
{
    int ii = 0;
    double a0, b0;

    for (ii = 0; ii <= len_freq; ii++) {
       CDivide( &complex_real[ii], &complex_imag[ii], b0, 0, a0, freq[ii] );
    } 
}

where

void CDivide( 
    double *z_real, double *z_imag, double a, double b, double c, double d) 
{
    double divisor = c*c + d*d; 
    *z_real = (a*c + b*d) / divisor; 
    *z_imag = (b*c - a*d) / divisor; 
}

The problem was the prototype for the CDivide function needs to reside in the file containing the CDivide and fn_v1p0 definitions, and not the file containing the main() function. Moving the prototype for CDivide to the file containing CDivide clears the error. Thanks to Adam Rosenfield for flushing out the issue with his comments in the question above.

Take 2 functions double CDivide_real(a,b,c,d) and CDivide_imag(a,b,c,d) or pass a double-pointer-array to CDivide like CDivide(double *x[2],a,b,c,d), and then

for (ii = 0; ii <= len_freq; ii++) {
   double *a[]={&complex_real[ii],&complex_imag[ii]};
   CDivide( a, b0, 0, a0, freq[ii] );
} 

and in CDivide

void CDivide( 
    double *x[2], double a, double b, double c, double d) 
{
    double divisor = c*c + d*d; 
    *x[0] = (a*c + b*d) / divisor; 
    *x[1] = (b*c - a*d) / divisor; 
}

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