• 回复
  • 收藏
  • 点赞
  • 分享
  • 发新帖

用函数指针传送数组指针变量的问题

最近在用AT89S52+12864LCD显示菜单时用到函数指针,想用函数指针调用不同函数,同时调用不同的数组,所以写了个例子,只要能实现功能.可是例中加!!!的地方就是编释不过,手上的资料又少,望高手指点一二!!在此谢过.
全部回复(10)
正序查看
倒序查看
xing1234
LV.6
2
2008-01-12 11:55
#include
#include
#include
#include
unsigned char  tongdata[12];
write_at2401(unsigned char a,unsigned char b,unsigned char c)
{
a=0;
b=0;
c=0;
}
void tong_gao(unsigned char chao,unsigned char max,unsigned char da,unsigned char *b)//
{
  unsigned char id;
  id=*(b+da);
if(chao>max)   
chao=0;  
write_at2401(0xA0,da,id);
chao=1;
}
/*****************主程序区****************************/
main()
{
  unsigned char      chao,chu,max,*se_data;
void (*p)(unsigned char ,unsigned char ,unsigned char ,unsigned char);
// void tong_gao(unsigned char ,unsigned char ,unsigned char ,unsigned char *);
chao=5;
chu=10;
max=12;
  p=tong_gao;
  se_data=tongdata;
  (*p)(chu,max,chao,se_data);//!!!!!!!!!!!!!!!!!!!!!!!!!
}
0
回复
xing1234
LV.6
3
2008-01-12 11:58
@xing1234
#include#include#include#includeunsignedchar  tongdata[12];write_at2401(unsignedchara,unsignedcharb,unsignedcharc){a=0;b=0;c=0;}voidtong_gao(unsignedcharchao,unsignedcharmax,unsignedcharda,unsignedchar*b)//{  unsignedcharid;  id=*(b+da);if(chao>max)  chao=0;  write_at2401(0xA0,da,id);chao=1;}/*****************主程序区****************************/main(){  unsignedchar      chao,chu,max,*se_data;void(*p)(unsignedchar,unsignedchar,unsignedchar,unsignedchar);//voidtong_gao(unsignedchar,unsignedchar,unsignedchar,unsignedchar*);chao=5;chu=10;max=12;  p=tong_gao;  se_data=tongdata;  (*p)(chu,max,chao,se_data);//!!!!!!!!!!!!!!!!!!!!!!!!!}
error C212: indirect call: parameters do not fit within registers
0
回复
2008-01-13 21:57
@xing1234
errorC212:indirectcall:parametersdonotfitwithinregisters
学习一下,怎么弄这个.是返回一个指针吗?还看不懂.返回指针,得赋值的吧?

我就会指针参数,往函数外输.
0
回复
xing1234
LV.6
5
2008-01-14 08:50
我的目的:
1、将变量chu,max,chao和指向数组tongdata[12]的数组指针变量se_data,通过函数指针p传到p指向的函数tong_gao;
2、通过改变数组指针变量se_data和函数指针p,我可以将不同的数组传送到不同的函数进行类似的运算.
0
回复
whatcall
LV.5
6
2008-01-14 09:13
@xing1234
errorC212:indirectcall:parametersdonotfitwithinregisters
参考:http://www.pconline.com.cn/pcedu/empolder/gj/c/0503/566020.html
(*p)和tong_gao最后一个传递参数类型不一致,一个是unsigned char,一个是unsigned char*.

更多资料在google中搜索“函数指针”.
0
回复
xing1234
LV.6
7
2008-01-14 11:23
@whatcall
参考:http://www.pconline.com.cn/pcedu/empolder/gj/c/0503/566020.html(*p)和tong_gao最后一个传递参数类型不一致,一个是unsignedchar,一个是unsignedchar*.更多资料在google中搜索“函数指针”.
谢谢!!加了*号后还是报的相同的错误.我再看看!
0
回复
xing1234
LV.6
8
2008-01-14 11:27
改了后的程序
#include
#include
#include
#include
unsigned char  tongdata[12];
write_at2401(unsigned char a,unsigned char b,unsigned char c)
{
a=0;
b=0;
c=0;
}
void tong_gao(unsigned char chao,unsigned char max,unsigned char da,unsigned char *b)//
{
  unsigned char id;
  id=*(b+da);
if(chao>max)   
chao=0;  
write_at2401(0xA0,da,id);
chao=1;
}
/*****************主程序区****************************/
main()
{
  unsigned char      chao,chu,max,*se_data;
void (*p)(unsigned char ,unsigned char ,unsigned char ,unsigned char *);//定义函数指针p
// void tong_gao(unsigned char ,unsigned char ,unsigned char ,unsigned char *);
chao=5;
chu=10;
max=12;
  p=tong_gao;        //p指向函数tong_gao
  se_data=tongdata;  //指针变量se_data指向数组tongdata[12]
  (*p)(chu,max,chao,se_data);//!!!!!!问题就在这里
}
0
回复
xing1234
LV.6
9
2008-01-14 11:38
@xing1234
改了后的程序#include#include#include#includeunsignedchar  tongdata[12];write_at2401(unsignedchara,unsignedcharb,unsignedcharc){a=0;b=0;c=0;}voidtong_gao(unsignedcharchao,unsignedcharmax,unsignedcharda,unsignedchar*b)//{  unsignedcharid;  id=*(b+da);if(chao>max)  chao=0;  write_at2401(0xA0,da,id);chao=1;}/*****************主程序区****************************/main(){  unsignedchar      chao,chu,max,*se_data;void(*p)(unsignedchar,unsignedchar,unsignedchar,unsignedchar*);//定义函数指针p//voidtong_gao(unsignedchar,unsignedchar,unsignedchar,unsignedchar*);chao=5;chu=10;max=12;  p=tong_gao;        //p指向函数tong_gao  se_data=tongdata;  //指针变量se_data指向数组tongdata[12]  (*p)(chu,max,chao,se_data);//!!!!!!问题就在这里}
se_data 是数组tongdata[12]的指针,问题就在这里.取消这个数组指针,改成(*p)(chu,max,chao);//!!!!!!!!!!!!!!!!!!!!!!!!!

void tong_gao(unsigned char chao,unsigned char max,unsigned char da,unsigned char *b)//去掉指针变量*b,就行了,但使用中这不是目的.
0
回复
whatcall
LV.5
10
2008-01-14 12:55
@xing1234
se_data是数组tongdata[12]的指针,问题就在这里.取消这个数组指针,改成(*p)(chu,max,chao);//!!!!!!!!!!!!!!!!!!!!!!!!!voidtong_gao(unsignedcharchao,unsignedcharmax,unsignedcharda,unsignedchar*b)//去掉指针变量*b,就行了,但使用中这不是目的.
我查了一下错误提示的解释,应该是C51编译器不支持带参数的函数指针调用,除非所有的参数可以用寄存器进行传递,而指向数组的指针实际上是数组的地址是16位,不可以用寄存器传递.看来你要想一下其他办法了.

Error C212
Indirect Call: Parameters Do Not Fit Within Registers
Summary   *** Error C212
    Indirect Call: Parameters Do Not Fit Within Registers


Description   An indirect function call through a pointer cannot contain actual parameters. An exception to this rule is when all parameters can be passed in registers. This is due to the method of parameter passing employed by Cx51. The name of the called function
0
回复
xing1234
LV.6
11
2008-01-14 14:24
@whatcall
我查了一下错误提示的解释,应该是C51编译器不支持带参数的函数指针调用,除非所有的参数可以用寄存器进行传递,而指向数组的指针实际上是数组的地址是16位,不可以用寄存器传递.看来你要想一下其他办法了.ErrorC212IndirectCall:ParametersDoNotFitWithinRegistersSummary  ***ErrorC212    IndirectCall:ParametersDoNotFitWithinRegistersDescription  Anindirectfunctioncallthroughapointercannotcontainactualparameters.Anexceptiontothisruleiswhenallparameterscanbepassedinregisters.ThisisduetothemethodofparameterpassingemployedbyCx51.Thenameofthecalledfunction
谢谢!!!!!
我也有这个想法,我在网上和书上查找了一些相关资料,都没有用函数指针传过数组指针变量,没人用过.应该是编释器不支持.
0
回复