struct TICRecord
{
char Card[20];
char timeString[20];
char EmpId[10];
int ;
int flag;
int cardTimes;
int consume;
int balance;
int reserved1;
int reserved2;
char reserved3[20];
char reserved4[101];
} ;
TICRecord * Records[16] = new TICRecord;
这样初始化不行!问高手门如何初始化Records[16]
{
char Card[20];
char timeString[20];
char EmpId[10];
int ;
int flag;
int cardTimes;
int consume;
int balance;
int reserved1;
int reserved2;
char reserved3[20];
char reserved4[101];
} ;
TICRecord * Records[16] = new TICRecord;
这样初始化不行!问高手门如何初始化Records[16]
Records = (TICRecord *)malloc(sizeof(TICRecord));
if(Records == NULL)
return;memset(Records, 0, sizeof(TICRecord));
TICRecord * Records[16] = {new TICRecord,new TICRecord,new TICRecord,……,new TICRecord};//里面十六个
或者这样:(作为数组指针)
TICRecord (*Records)[16] = (TICRecord (*)[16])new TICRecord[16]; //这样可以改变所指向的空间的值,但不能像指针那样改变自身。
最后,就是:
TICRecord *Records[16];
for循环初始化了。
for(int i = 0; i < 16; i++)
{
Records[i] = new TICRecord;
}
BatchReadRecord(hPort,Records[16],16);
这样可以吗?应该还要释放内存吧,接着怎么做
这条语句比较诡异,一般来说是调用错误了,函数参数类型报过来听听。其实,我觉得吧,你可能根本不需要使用数组,可以直接
TICRecord * Records = new TICRecord[16];这样多好,释放的时候就delete[] Records;
那需要像指针数组那样再来一个循环去释放……
function BatchReadRecord(hPort: THandle; var Records: array of TICRecord{; ArraySize: Integer}): Integer; 参数说明:
hPort: 端口句柄, 调用OpenCommPort函数得到,需要联机
Records: 返回记录数组, 最少需要16个单元
ArraySize: Delphi不需要,如果是VB或是C++需要传数组大小参数
TICRecord * Records = new TICRecord[16];
//……
BatchReadRecord(hPort,Records,16);
//千万不要写Records[16],按照这种定义编译都通不过,就算按照你原先指针数组的定义,可以通过编译,也犯了两个错误:1、数组下标越界,2、空间分配不足
delete[] Records;或者new都不要了,这样:
TICRecord Records[16];
//……
BatchReadRecord(hPort,Records,16);
Unhandled exception in EastR.exe(NTDLL.DLL):0xC000005:Access Violation
//……
BatchReadRecord(hPort,Records,16);
这样写,语法上还是语义上都没有问题,你可以自己写函数去访问数据测试一下。
如果出现问题,那么问题必定发生在其他地方,那是LZ的找BUG方向错误了……
在一个不存在错误的地方找错,除了耗费时间,是不会有其他结果的。