用函数指针传送数组指针变量的问题
最近在用AT89S52+12864LCD显示菜单时用到函数指针,想用函数指针调用不同函数,同时调用不同的数组,所以写了个例子,只要能实现功能.可是例中加!!!的地方就是编释不过,手上的资料又少,望高手指点一二!!在此谢过.
全部回复(10)
正序查看
倒序查看
现在还没有回复呢,说说你的想法
#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);//!!!!!!!!!!!!!!!!!!!!!!!!!
}
#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
#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
回复
提示
改了后的程序
#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);//!!!!!!问题就在这里
}
#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
改了后的程序#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,就行了,但使用中这不是目的.
void tong_gao(unsigned char chao,unsigned char max,unsigned char da,unsigned char *b)//去掉指针变量*b,就行了,但使用中这不是目的.
0
回复
提示
@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
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
回复
提示
@whatcall
我查了一下错误提示的解释,应该是C51编译器不支持带参数的函数指针调用,除非所有的参数可以用寄存器进行传递,而指向数组的指针实际上是数组的地址是16位,不可以用寄存器传递.看来你要想一下其他办法了.ErrorC212IndirectCall:ParametersDoNotFitWithinRegistersSummary ***ErrorC212 IndirectCall:ParametersDoNotFitWithinRegistersDescription Anindirectfunctioncallthroughapointercannotcontainactualparameters.Anexceptiontothisruleiswhenallparameterscanbepassedinregisters.ThisisduetothemethodofparameterpassingemployedbyCx51.Thenameofthecalledfunction
谢谢!!!!!
我也有这个想法,我在网上和书上查找了一些相关资料,都没有用函数指针传过数组指针变量,没人用过.应该是编释器不支持.
我也有这个想法,我在网上和书上查找了一些相关资料,都没有用函数指针传过数组指针变量,没人用过.应该是编释器不支持.
0
回复
提示