函数指针、函数意外结束问题

Proteas 2005-11-15 10:54:42
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<assert.h>
/*********************************************/
void main()
{
clock_t start,end,over;
int convert(int m);
void process( int size,unsigned char *source,unsigned char *destination,int (*mm)( int));
FILE *file;
int width,height,size;
char *name=NULL;
int a=1;
while(a)
{
printf("Please input your file name: \n");
scanf("%s",name);
if((file=fopen(name,"rb"))=NULL)
printf("Input error!\nEnter 0 to exit.\n");
scanf("%d",&a);
}
a=1;
while(a)
{
printf("Please input the width of your file:\n");
scanf("%d",&width);
if(width<=0)
printf("Input error!\nEnter 0 to exit.\n");
scanf("%d",&a);
}
a=1;
while(a)
{
printf("Please input the height of your file:\n");
scanf("%d",&height);
if(width<=0)
printf("Input error!\nEnter 0 to exit.\n");
scanf("%d",&a);
}
size=(int)(width*height);
unsigned char *source,*destination;
source=(unsigned char *)calloc(size,sizeof(char));
destination=(unsigned char *)calloc(size,sizeof(char));
fread(source,size,1,file);
fclose(file);
start=clock();
end=clock();
over=end-start;
start=clock();
process(size,source,destination, convert);
end=clock();
printf("time: %4.2f s\n",(float)(end-start-over)/CLK_TCK);
fwrite(destination,size,1,file);
fclose(file);
free(source);
free(destination);
}
/*********************************************/
/********************************************/
void process( int size,unsigned char *source,unsigned char *destination, int (*mm)( int ))
{
int i=0,temp;
for(i;i<size;i++)
{
temp=(*mm)(*source(i));//报错:term does not evaluate to a function

if(temp<0)
temp=0;
if(temp>255)
temp=255;
*destination(i)=temp;//报错:term does not evaluate to a function
}
/**************************************************/
/**************************************************/
int convert(int m)
{
int Gray_Level,a=1,back;//报错:local function definitions are illegal
while(a)

{
printf("Please input the Gray_Level of your image:\n");
scanf("%d",&Gray_Level);
if(Gray_Level<=0)
printf("Input error!\nEnter 0 to exit.\n");
scanf("%d",&a);
}
back=Gray_Level-1-m);
return back;
}//报错:unexpected end of file found
编译时提示有四个错误,我在程序中标明了,我不知道错在哪里,该怎么改,谢谢。
...全文
232 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Proteas 2005-11-16
  • 打赏
  • 举报
回复
fflush(stdin);
exit(-1);
memset(name, 0, sizeof(name));
temp=(*mm)(source[i])
这些地方不懂。
Proteas 2005-11-16
  • 打赏
  • 举报
回复
谢谢大家。
megaboy 2005-11-16
  • 打赏
  • 举报
回复
source是unsigned char类型的,而mm的形参是int的,要转换过来:

temp=(*mm)((int)source[i]);
Proteas 2005-11-16
  • 打赏
  • 举报
回复
感觉自己的基础还是太差,我想看些c语言比较高级用法的基础书,可以推荐基本吗?电子书也行,wangwei_001@126.com
Proteas 2005-11-16
  • 打赏
  • 举报
回复
unsigned char *source,*destination;
source=(unsigned char *)calloc(size,sizeof(char));
destination=(unsigned char *)calloc(size,sizeof(char));
//这里定义了两个指针
void process( int size,unsigned char *source,unsigned char *destination, int (*mm)( int ))
{
int i=0,temp;
for(i;i<size;i++)
{
temp=(*mm)(source[i]);//报错:term does not evaluate to a function

if(temp<0)
temp=0;
if(temp>255)
temp=255;
destination[i]=temp;//报错:term does not evaluate to a function
}
}int convert(int m)
{
int Gray_Level,a=1,back;//报错:local function definitions are illegal
while(a)
{
a = 0;
printf("Please input the Gray_Level of your image:\n");
scanf("%d",&Gray_Level);
if(Gray_Level<=0)
{
printf("Input error!\nEnter 0 to exit.\n");
fflush(stdin);
scanf("%d",&a);
if(a == 0)
{
exit(-1);
}
}
}
back=Gray_Level-1-m;
return back;
}//
/******************************************/
还是不明白,
temp=(*mm)(source[i]);//
temp=(*mm)(*source(i));//
source 已经指向了数组首地址,这两个有什么不同?
(*mm)(int)的参数是一个数据,上面的参数都是同一个数据,一个用指针一个用数组而已,谢谢。

wanguodu 2005-11-16
  • 打赏
  • 举报
回复
fflush(stdin); //清除标准输入中的残留字符,以便后面的scanf能得到正确输入
exit(-1); //出现了无法继续的情况,如文件打不开,并且用户也同意了,因此退出程序
memset(name, 0, sizeof(name));
-----------------
清零是为了后面的:printf("Cannot open file %s\n", name); 因为可能出现用户直接输入回车的情况,不清零的话,这句会打印乱码

temp=(*mm)(source[i])这个 megaboy已经说过了。
megaboy 2005-11-16
  • 打赏
  • 举报
回复
source(i)表示调用函数,source[i]是数组引用。
Proteas 2005-11-16
  • 打赏
  • 举报
回复
temp=(*mm)(source[i]);//
temp=(*mm)(*source(i));//
source 已经指向了数组首地址,这两个有什么不同?
wanguodu 2005-11-15
  • 打赏
  • 举报
回复
LZ错误实在太多,就不一一指出了,下面是修改了一些明显错误后的程序,LZ有不懂的再问吧:

#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
/*********************************************/
void main()
{
clock_t start,end,over;
int convert(int m);
void process( int size,unsigned char *source,unsigned char *destination,int (*mm)( int));
FILE *file;
int width,height,size;
char name[80];//char *name=NULL;
int a=1;

memset(name, 0, sizeof(name));

while(a != 0)
{
a = 0;

printf("Please input your file name: ");
scanf("%s", name);
if((file=fopen(name,"r+b"/* 错,应以读和写方式打开, "rb"*/))/*错,=*/==NULL)
{
printf("Cannot open file %s\n", name);
printf("Type 0 to exit the program, other value to try again\n");

fflush(stdin);
scanf("%d", &a);
if(a == 0)
{
exit(-1);
}
}
}
a=1;
while(a)
{
a = 0;

printf("Please input the width of your file:\n");
scanf("%d", &width);
if(width <= 0)
{
printf("width input illegal\n");
printf("Type 0 to exit the program, other value to try again\n");
fflush(stdin);
scanf("%d",&a);
if(a == 0)
{
exit(-1);
}
}
}
a=1;
while(a)
{
a = 0;
printf("Please input the height of your file:\n");
scanf("%d",&height);
if(height<=0)
{
printf("height input illegal\n");
printf("Type 0 to exit the program, other value to try again\n");
fflush(stdin);
scanf("%d",&a);
if(a == 0)
{
exit(-1);
}
}
}
size=(int)(width*height);
unsigned char *source,*destination;
source=(unsigned char *)calloc(size,sizeof(char));
destination=(unsigned char *)calloc(size,sizeof(char));
fread(source,size,1,file);
//错,后面还要写文件呢,fclose(file);
start=clock();
end=clock();
over=end-start;
start=clock();
process(size,source,destination, convert);
end=clock();
printf("time: %4.2f s\n",(float)(end-start-over)/CLK_TCK);
fwrite(destination,size,1,file);
fclose(file);
free(source);
free(destination);
}
/*********************************************/
/********************************************/
void process( int size,unsigned char *source,unsigned char *destination, int (*mm)( int ))
{
int i=0,temp;
for(i;i<size;i++)
{
temp=(*mm)(source[i]);//报错:term does not evaluate to a function

if(temp<0)
temp=0;
if(temp>255)
temp=255;
destination[i]=temp;//报错:term does not evaluate to a function
}
}
/**************************************************/
/**************************************************/
int convert(int m)
{
int Gray_Level,a=1,back;//报错:local function definitions are illegal
while(a)
{
a = 0;
printf("Please input the Gray_Level of your image:\n");
scanf("%d",&Gray_Level);
if(Gray_Level<=0)
{
printf("Input error!\nEnter 0 to exit.\n");
fflush(stdin);
scanf("%d",&a);
if(a == 0)
{
exit(-1);
}
}
}
back=Gray_Level-1-m;
return back;
}//报错:unexpected end of file found
Proteas 2005-11-15
  • 打赏
  • 举报
回复
我加了半个括号后,前两个错误还在。
void process( int size,unsigned char *source,unsigned char *destination, int (*mm)( int ))
{
int i=0,temp;
for(i;i<size;i++)
{
temp=(*mm)(*source(i));//报错:term does not evaluate to a function

if(temp<0)
temp=0;
if(temp>255)
temp=255;
*destination(i)=temp;//报错:term does not evaluate to a function
}
}

怎么上面这个函数没有结束的大括号啊?
megaboy 2005-11-15
  • 打赏
  • 举报
回复
temp=(*mm)(*source(i));//报错:term does not evaluate to a function
*destination(i)=temp;//报错:term does not evaluate to a function
------------------------------------------------------------------
source和destination都是指针,怎么能当作函数指示符使用呢,应该是:

temp=(*mm)(source[i]);
destination[i]=temp;



int Gray_Level,a=1,back;//报错:local function definitions are illegal
----------------------------------------------------------------------
由于process函数缺了一个大括号},至使编译器把process函数体延伸到int convert(int m)后面去了,而int convert(int m)是一个函数定义,变成了在process函数体内定义一个函数,但C是不支持函数嵌套的,因此会说函数定义非法。后面的unexpected end of file found报错也是这个原因,把大括号}加回去应该就不会了。
Mr_Yang 2005-11-15
  • 打赏
  • 举报
回复
temp=(*mm)(*source(i));//报错:term does not evaluate to a function

这个不知道是什么意思。
mm是指向函数得指针,而source是char型的。。。。。。。。
snowbirdfly 2005-11-15
  • 打赏
  • 举报
回复
恩~~
是不是这个问题阿~~
晨星 2005-11-15
  • 打赏
  • 举报
回复
void process( int size,unsigned char *source,unsigned char *destination, int (*mm)( int ))
{
int i=0,temp;
for(i;i<size;i++)
{
temp=(*mm)(*source(i));//报错:term does not evaluate to a function

if(temp<0)
temp=0;
if(temp>255)
temp=255;
*destination(i)=temp;//报错:term does not evaluate to a function
}

怎么上面这个函数没有结束的大括号啊?

69,382

社区成员

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

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