简体   繁体   English

如何在ANSI C程序中将数组的元素作为参数传递给函数进行修改?

[英]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. 我很难理解如何将数组的ELEMENT传递给函数并让该函数更新该元素。 My guess is as below, but produces the following from gcc: "warning: conflicting types for 'CDivide'" . 我的猜测如下,但是从gcc: "warning: conflicting types for 'CDivide'"产生了以下内容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 . main()函数定义有问题的数组,并将它们(作为参考)作为complex_realcomplex_imag传递给fn_v1p0 ,在函数fn_v1p0的函数定义fn_v1p0它们声明为指针。 The main() includes prototypes for these functions, as follows. main()包括这些函数的原型,如下所示。

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. 但是, main()位于一个文件中,而其他两个函数位于另一个文件中。 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 . 我需要执行复杂的除法(ANSI C中不支持),这是功能CDivide的目的。 There's more to the fn_v1p0 function, but I reduced it to only what's necessary to show for this question. fn_v1p0功能还有更多,但我将其简化为仅显示此问题所需的内容。

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. 我已经将complex_real[ii]视为简单的双CDivide值,希望如果在其前面加上&,则可以如图所示编写CDivide 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. 问题在于CDivide函数的原型需要驻留在包含CDivide和fn_v1p0定义的文件中,而不是驻留在包含main()函数的文件中。 Moving the prototype for CDivide to the file containing CDivide clears the error. 将CDivide的原型移至包含CDivide的文件可清除该错误。 Thanks to Adam Rosenfield for flushing out the issue with his comments in the question above. 感谢亚当·罗森菲尔德(Adam Rosenfield)用上面问题中的评论消除了这个问题。

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 取2个函数对CDivide_real(a,b,c,d)和CDivide_imag(a,b,c,d)进行双重处理,或者将一个双指针数组传递给CDivide,例如CDivide(double * x [2],a,b,c ,d),然后

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 和在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; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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