用函數(shù)指針傳送數(shù)組指針變量的問題
最近在用AT89S52+12864LCD顯示菜單時用到函數(shù)指針,想用函數(shù)指針調(diào)用不同函數(shù),同時調(diào)用不同的數(shù)組,所以寫了個例子,只要能實現(xiàn)功能.可是例中加!!!的地方就是編釋不過,手上的資料又少,望高手指點一二!!在此謝過.
全部回復(fù)(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;
}
/*****************主程序區(qū)****************************/
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;
}
/*****************主程序區(qū)****************************/
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
回復(fù)
@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;}/*****************主程序區(qū)****************************/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
回復(fù)
改了后的程序
#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;
}
/*****************主程序區(qū)****************************/
main()
{
unsigned char chao,chu,max,*se_data;
void (*p)(unsigned char ,unsigned char ,unsigned char ,unsigned char *);//定義函數(shù)指針p
// void tong_gao(unsigned char ,unsigned char ,unsigned char ,unsigned char *);
chao=5;
chu=10;
max=12;
p=tong_gao; //p指向函數(shù)tong_gao
se_data=tongdata; //指針變量se_data指向數(shù)組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;
}
/*****************主程序區(qū)****************************/
main()
{
unsigned char chao,chu,max,*se_data;
void (*p)(unsigned char ,unsigned char ,unsigned char ,unsigned char *);//定義函數(shù)指針p
// void tong_gao(unsigned char ,unsigned char ,unsigned char ,unsigned char *);
chao=5;
chu=10;
max=12;
p=tong_gao; //p指向函數(shù)tong_gao
se_data=tongdata; //指針變量se_data指向數(shù)組tongdata[12]
(*p)(chu,max,chao,se_data);//!!!!!!問題就在這里
}
0
回復(fù)
@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;}/*****************主程序區(qū)****************************/main(){ unsignedchar chao,chu,max,*se_data;void(*p)(unsignedchar,unsignedchar,unsignedchar,unsignedchar*);//定義函數(shù)指針p//voidtong_gao(unsignedchar,unsignedchar,unsignedchar,unsignedchar*);chao=5;chu=10;max=12; p=tong_gao; //p指向函數(shù)tong_gao se_data=tongdata; //指針變量se_data指向數(shù)組tongdata[12] (*p)(chu,max,chao,se_data);//!!!!!!問題就在這里}
se_data 是數(shù)組tongdata[12]的指針,問題就在這里.取消這個數(shù)組指針,改成(*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
回復(fù)
@xing1234
se_data是數(shù)組tongdata[12]的指針,問題就在這里.取消這個數(shù)組指針,改成(*p)(chu,max,chao);//!!!!!!!!!!!!!!!!!!!!!!!!!voidtong_gao(unsignedcharchao,unsignedcharmax,unsignedcharda,unsignedchar*b)//去掉指針變量*b,就行了,但使用中這不是目的.
我查了一下錯誤提示的解釋,應(yīng)該是C51編譯器不支持帶參數(shù)的函數(shù)指針調(diào)用,除非所有的參數(shù)可以用寄存器進行傳遞,而指向數(shù)組的指針實際上是數(shù)組的地址是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
回復(fù)
@whatcall
我查了一下錯誤提示的解釋,應(yīng)該是C51編譯器不支持帶參數(shù)的函數(shù)指針調(diào)用,除非所有的參數(shù)可以用寄存器進行傳遞,而指向數(shù)組的指針實際上是數(shù)組的地址是16位,不可以用寄存器傳遞.看來你要想一下其他辦法了.ErrorC212IndirectCall:ParametersDoNotFitWithinRegistersSummary ***ErrorC212 IndirectCall:ParametersDoNotFitWithinRegistersDescription Anindirectfunctioncallthroughapointercannotcontainactualparameters.Anexceptiontothisruleiswhenallparameterscanbepassedinregisters.ThisisduetothemethodofparameterpassingemployedbyCx51.Thenameofthecalledfunction
謝謝!!!!!
我也有這個想法,我在網(wǎng)上和書上查找了一些相關(guān)資料,都沒有用函數(shù)指針傳過數(shù)組指針變量,沒人用過.應(yīng)該是編釋器不支持.
我也有這個想法,我在網(wǎng)上和書上查找了一些相關(guān)資料,都沒有用函數(shù)指針傳過數(shù)組指針變量,沒人用過.應(yīng)該是編釋器不支持.
0
回復(fù)