关于一个函数指针数组(error: array type has incomplete element type)
麻烦各位达人,看看下面这个代码错在哪里,应该如何修正错误:
/* ex11, ch 14 */
#include <stdio.h>
#include <math.h>
#define NUM10
#define VAR 4
typedef double (*FUN[])(double); //或者是这里出了问题?
void transform(double , double , int, double (*)(double));
double twice(double);
double half(double);
int main(void)
{
double source[NUM] = {0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0};
double target[NUM];
FUN funcs[VAR] = {sqrt, sin, twice, half}; //编译器gcc在这里报错:
//error: array type has incomplete element type
for(int i = 0; i < VAR; i++)
for(int j = 0; j < NUM; j++)
transform(source[j], target[j], NUM, funcs[i](source[j]));
return 0;
}
void transform(double source, double target, int n, double (*func)(double))
{
for(int i = 0; i < n; i++)
target = func(source);
}
double twice(double x)
{
return 2.0 * x;
}
double half(double x)
{
return x/2.0;
}
/* ex11, ch 14 */
#include <stdio.h>
#include <math.h>
#define NUM10
#define VAR 4
typedef double (*FUN)(double);
void transform(double* , double* , int, double (*)(double));
double twice(double);
double half(double);
int main(void)
{
double source[NUM] = {0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0};
double target[NUM];
FUN funcs[VAR] = {sqrt, sin, twice, half};
int i;
for( i = 0; i < VAR; i++)
transform(source, target, NUM, funcs[i]);
return 0;
}
void transform(double* source, double* target, int n, double (*func)(double))
{
int i;
for( i = 0; i < n; i++)
target[i] = func(source[i]);
}
double twice(double x)
{
return 2.0 * x;
}
double half(double x)
{
return x/2.0;
}
typedef double (*FUN)(double);
for(int i = 0; i < VAR; i++)
{
if(funcs[i] != NULL)
{
for(int j = 0; j < NUM; j++)
{
transform(source[j], target[j], NUM, funcs[i]);
}
}
}
typedef double (*FUN)(double);
void transform(double , double , int, FUN func);
double twice(double);
double half(double);
int main(void)
{
int i,j;
double source[NUM] = {0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0}; 20
double target[NUM];
FUN abc[VAR] = {sqrt, sin, twice, half};
for(i = 0; i < VAR; i++)
{
for(j = 0; j < NUM; j++)
{
transform(source[j], target[j], NUM, abc[i]);
}
}
return 0;
}
void transform(double source, double target, int n, FUN func)
{
for(int i = 0; i < n; i++)
target = func(source);
}
double twice(double x)
{
return 2.0 * x;
}
double half(double x)
{
return x/2.0;
}