关于一个函数指针数组(error: array type has incomplete element type)

mcmay 2014-01-18 06:57:08
麻烦各位达人,看看下面这个代码错在哪里,应该如何修正错误:

/* ex11, ch 14 */

#include <stdio.h>
#include <math.h>

#define NUM 10
#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;
}

谢谢!
...全文
1139 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
mcmay 2014-01-19
  • 打赏
  • 举报
回复
引用 3 楼 JieWinli 的回复:

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;
}
谢谢,你指出的跟楼上两位一样都很准确,就是typedef那里的问题了!
mcmay 2014-01-19
  • 打赏
  • 举报
回复
引用 2 楼 KenZhang1031 的回复:
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那里的问题!
mcmay 2014-01-19
  • 打赏
  • 举报
回复
引用 1 楼 mujiok2003 的回复:
不知道是不是这个效果
/* ex11, ch 14 */

#include <stdio.h>
#include <math.h>


#define NUM		10
#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那里出了问题,应该先定义指针类型为函数指针而非直接定义成函数指针数组。谢谢!
jiewinli 2014-01-18
  • 打赏
  • 举报
回复
不知道是不是你想要的结果
jiewinli 2014-01-18
  • 打赏
  • 举报
回复

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;
}
Mr. Code 2014-01-18
  • 打赏
  • 举报
回复
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]);
			}
		}
	}
mujiok2003 2014-01-18
  • 打赏
  • 举报
回复
不知道是不是这个效果
/* ex11, ch 14 */

#include <stdio.h>
#include <math.h>


#define NUM		10
#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;
}

69,379

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧