有个函数的定义是这样的
extern "C" DLLSMEPAPI int __cdecl SMEP_Submit(
int IN_ConnectID,
int IN_DestCount,
const char Dest_Terminal_ID[][22]);
因为要发送的Dest_Terminal_ID(手机号)是不固定的.所以我用
手机号1:手机号2...这样的格式存放,
现在需要将这样的字串转成数组
我是这样做的
strline就是字串如"13600000000:13700000000:13800000000"i=strline.GetLength();
strline1=strline.Replace(":","");
i-=strline1.GetLength();
i++;char** m_Dest_Terminal=new char*[22];
for(int j=0;j<=i;j++)
m_Dest_Terminal[i]=new char[22];
i=0;
for ( p=strtok(ss,":");p!=NULL;p=strtok(NULL, ":"))
{
strcpy(m_Dest_Terminal[i], p);
i++;
}m_iStatus = SMEP_Submit(m_iConnectHand,i,m_Dest_Terminal);但编译会出现
rror C2664: 'SMEP_Submit' : cannot convert parameter 3 from 'char ** ' to 'const char [][22]' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
错误.请教我的代码该如何改进? 或是有没有其它的办法能实现这样的功能.
谢谢,很急,接触VC时间不长,请高手帮帮忙....
extern "C" DLLSMEPAPI int __cdecl SMEP_Submit(
int IN_ConnectID,
int IN_DestCount,
const char Dest_Terminal_ID[][22]);
因为要发送的Dest_Terminal_ID(手机号)是不固定的.所以我用
手机号1:手机号2...这样的格式存放,
现在需要将这样的字串转成数组
我是这样做的
strline就是字串如"13600000000:13700000000:13800000000"i=strline.GetLength();
strline1=strline.Replace(":","");
i-=strline1.GetLength();
i++;char** m_Dest_Terminal=new char*[22];
for(int j=0;j<=i;j++)
m_Dest_Terminal[i]=new char[22];
i=0;
for ( p=strtok(ss,":");p!=NULL;p=strtok(NULL, ":"))
{
strcpy(m_Dest_Terminal[i], p);
i++;
}m_iStatus = SMEP_Submit(m_iConnectHand,i,m_Dest_Terminal);但编译会出现
rror C2664: 'SMEP_Submit' : cannot convert parameter 3 from 'char ** ' to 'const char [][22]' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
错误.请教我的代码该如何改进? 或是有没有其它的办法能实现这样的功能.
谢谢,很急,接触VC时间不长,请高手帮帮忙....
http://www.china-askpro.com/msg25/qa74.shtml另外,你的代码中还有不少问题。你可能应该用CString::Remove,而不是CString::Replace;还有,复制字符串用strcpy会把字符串末尾的\0也考进去,还是用memcpy比较好。
m_Dest_Terminal[i]=new char[22];上面这段代码也是有问题的
修改成
char** m_Dest_Terminal=new (char*)[22];
请高手指教!
char* m_Dest_Terminal[]=new char*[i];
#include <windows.h>
#include <iostream>
using namespace std;#define DLLSMEPAPI// 模拟函数
extern "C" DLLSMEPAPI int __cdecl SMEP_Submit(
int IN_ConnectID,
int IN_DestCount,
const char Dest_Terminal_ID[][22]);// 假设以""作为数组结尾
extern "C" DLLSMEPAPI int __cdecl SMEP_Submit(
int IN_ConnectID,
int IN_DestCount,
const char Dest_Terminal_ID[][22])
{
int i = 0;
for(i = 0; Dest_Terminal_ID[i][0] != '\0'; ++i)
{
cout << Dest_Terminal_ID[i] << endl;
}
return i;
}int main()
{
// 原始数据
char strline[] = "13600000000:13700000000:13800000000";
// 计算字符串个数
int nNum = 0;
for(int i=0; strline[i] != 0; ++i)
{
if((i==0)&&(strline[i] != ':'))
{
nNum++;
}
if((strline[i] == ':')&&(strline[i+1] != '\0'))
{
nNum++;
}
} // 分解
typedef char DestBuf[22]; DestBuf* buf = new DestBuf[nNum+1]; //!!! 必须一次分配,不能分多次分配内存。 if(buf == 0)
{
return 0;
} char* p = 0;
int nIndex = 0;
for ( p=strtok(strline,":");p!=NULL;p=strtok(NULL, ":"))
{
strcpy(buf[nIndex], p);
nIndex++;
if(nIndex>nNum)
{
break;
}
}
buf[nIndex][0] = 0; // 调用
int m_iStatus = 0;
int m_iConnectHand = 0;
int m_i = 0; m_iStatus = SMEP_Submit(m_iConnectHand,m_i,buf); delete[] buf;
buf = 0; return 0;
}// 运行结果:
/*
13600000000
13700000000
13800000000
*/