注册 登录
编程论坛 C语言论坛

函数调用问题

a187326700 发布于 2021-07-11 05:42, 2123 次点击
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define    LEN    20

void transform(double *source, double *target, int n, double (*fp)(double));

int    main(void)
{
    double    source[LEN];//源数组
    double    target[LEN];//目标数组
    double    num;
    transform(source, target, LEN, sin(num)); //这里提示 错误
    return    0;
}
void transform(double sou[LEN], double tar[LEN], int LEN, double sin(double num))
{
    int i;
    srand((unsigned)time(NULL));
    for(i = 0; i < LEN; i++)        //为源数组赋值
    {
        sou[i] = rand() % 100 +1;
        printf("%.2f  ", sou[i]);
    }
    for(i = 0; i < LEN; i++)        //把源数组值求正玄值赋值给目标数组
    {
        tar[i] = sin(sou[i]);
        printf("%.2f  ", tar[i]);
    }        
}

[Error] cannot convert 'double' to 'double (*)(double)' for argument '4' to 'void transform(double*, double*, int, double (*)(double))'
6 回复
#2
apull2021-07-11 11:59
程序代码:

int main(void)
{
    double    source[LEN];//源数组
    double    target[LEN];//目标数组
    double    num;   
    transform(source, target, LEN, &sin); //这里提示 错误
    return    0;
}
void transform(double sou[], double tar[], int LEN, double (*fp)(double))
{
    int i;
    srand((unsigned)time(NULL));
    for(i = 0; i < LEN; i++)        //为源数组赋值
    {
        sou[i] = rand() % 100 +1;
        printf("%.2f  ", sou[i]);
    }
    printf("\n");
    for(i = 0; i < LEN; i++)        //把源数组值求正玄值赋值给目标数组
    {
        tar[i] = fp(sou[i]*3.14159265/180);//库函数 double sin(double x) 参数是弧度角。
        printf("%.2f  ", tar[i]);
    }        
}

#3
rjsp2021-07-11 23:41
此外,
从 transform 这个名字来看,不应该将 给sou赋值 的代码放入其中
#4
a1873267002021-07-12 10:39
怎么改才能编译通过呀,二楼的代码也编译不过呀。。。
只有本站会员才能查看附件,请 登录



以上是改成二楼的代码提示的信息
#5
zbjzbj2021-07-12 12:01
#define    LEN    20

void transform(double sou[LEN], double tar[LEN], int LEN, double sin(double num))

LEN 冲突
#6
a1873267002021-07-12 12:32
回复 5楼 zbjzbj
能说明白点吗,改完能编译通过了

但还是不太明白原因

[此贴子已经被作者于2021-7-12 12:35编辑过]

#7
rjsp2021-07-12 13:10
按照我的猜测(只是“猜测”,因为题主没有给出“题目要求”),应该是

程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

void transform( const double* restrict src, double* restrict dst, size_t n, double(*fp)(double) )
{
    for( size_t i=0; i!=n; ++i )
        dst[i] = fp( src[i] );
}

int main( void )
{
    enum { LEN = 20 };
    double src[LEN];
    srand( (unsigned)time(NULL) );
    for( size_t i=0; i!=LEN; ++i )
        src[i] = (int)(rand()/(RAND_MAX+1.)*100) + 1;

    double dst[LEN];
    transform( src, dst, LEN, &sin );

    for( size_t i=0; i!=LEN; ++i )
        printf( "%5.2f%c", src[i], " \n"[i+1==LEN] );
    for( size_t i=0; i!=LEN; ++i )
        printf( "%+5.2f%c", dst[i], " \n"[i+1==LEN] );
}


一种可能的输出是
84.00 42.00 3.00 34.00 9.00 4.00 25.00 78.00 18.00 17.00 61.00 54.00 22.00 68.00 48.00 93.00 39.00 18.00 4.00 95.00
+0.73 -0.92 +0.14 +0.53 +0.41 -0.76 -0.13 +0.51 -0.75 -0.96 -0.97 -0.56 -0.01 -0.90 -0.77 -0.95 +0.96 -0.75 -0.76 +0.68


[此贴子已经被作者于2021-7-12 13:15编辑过]

1