|
5#
楼主 |
发表于 2009-12-9 17:29:51
|
只看该作者
re
问题12: 如何在TCL中将数组作为参数传入过程中
案例:
要测试如下函数CTFCToTFI
typedef unsigned int UINT ;
typedef unsigned short USHORT;
typedef unsigned char UCHAR;
void CTFCToTFI (UINT uwCTFC,UCHAR ucTrCHNum,UINT uwP[],USHORT uhwInterTFI[])
{//根据uwCTFC,ucTrCHNum,uwP[]计算uhwInterTFI[].
//uwP[],uhwInterTFI[]是数组,且长度由ucTrCHNum确定。如何传递参数uwp, uhwIterTFI?
UINT m;
UINT uwTemp;
int i;
m = uwCTFC;
i = ucTrCHNum;
for(i=ucTrCHNum-1; i>=0; i--)
{
uwTemp = m / uwP;
/*因为m,uwP[i-1]均为正值,所以不需检验负数情况*/
uhwInterTFI = uwTemp;/*(USHORT)(floor(m/uwP));*/
m = m % uwP;
}
return;
}
解决方案:
#include "tcl.h"
int Tcl_InitApp(Tcl_Interp* interp);
int TestCmd(ClientData clientdata,Tcl_Interp* interp,int argc,char * argv[]);
typedef unsigned int UINT ;
typedef unsigned short USHORT;
typedef unsigned char UCHAR;
USHORT pTFI[128];
void CTFCToTFI (UINT uwCTFC,UINT ucTrCHNum,UINT uwP[],USHORT uhwInterTFI[]) //被测函数
{
...
}
void main(int argc,char* argv[]) //主函数
{
Tcl_Main(argc,argv,Tcl_InitApp);
return;
}
int Tcl_InitApp(Tcl_Interp* interp)
{
Tcl_Init(interp);
Tcl_CreateCommand(interp,"test",TestCmd,NULL,NULL); //注册扩展命令test
return TCL_OK;
}
int TestCmd(ClientData clientdata,Tcl_Interp* interp,int argc,char * argv[])
//扩展命令test
{
UINT i,CTFC,Num=0;
UINT pwP[128];
UCHAR temp[80];
//判断参数类型;
//获取需传入被测函数的参数值;
if(TCL_OK!=Tcl_GetInt(interp,argv[1],&CTFC))
{
sprintf(interp->result,"Expect integer but got %s",argv[1]);
return TCL_ERROR;
}
if(TCL_OK!=Tcl_GetInt(interp,argv[2],&Num))
{
sprintf(interp->result,"Expect integer but got %s",argv[2]);
return TCL_ERROR;
}
for (i=0;i<Num;i++)
{
if(TCL_OK!=Tcl_GetInt(interp
,argv[i+3],&pwP))
{
sprintf(interp->result,"Expect integer but got %s",argv[i+3]);
return TCL_ERROR;
}
}
CTFCToTFI(CTFC,Num,pwP,pTFI); //向被测函数传入参数
for (i=0;i<Num;i++)
{
sprintf(temp, "%d\n", pTFI);
strcat(interp->result, temp); //输出结果
}
return TCL_OK;
}
测试脚本和结果如下:
% test 25 2 1 2 //其中uwCTFC=25,ucTrCHNum=2,uwP[0]=1,uwP[1]=2
1
12 //uhwInterTFI[0]=1,uhwInterTFI[1]=12 |
|