【发布时间】:2022-12-31 19:05:20
【问题描述】:
在 64 位机器中将 char* 转换为 am int 时遇到问题。 我知道问题出在 64 位 sizeof(char*) 是 8 而 sizeof(int) 是 4。 这是代码:
void books::update()
{
int b_id[100],qty[100],i=0,max;
stmt.str("");
stmt<<" Select book_id,qty from purchase where recieves ='T' and inv IS NULL;";
query=stmt.str();
q =query.c_str();
mysql_query(conn,q);
res_set=mysql_store_result(conn);
stmt.str("");
stmt<<" Update purchases set inv=1 where recieves ='T' and inv is NULL";
query=stmt.str();
q=query.c_str();
mysql_query(conn,q);
while((row=mysql_fetch_row(res_set))!=NULL)
{
b_id[i]=(int)row[0];
qty[i]= (int)row[1];
i++;
}
max=i;
for(i =0;i<max;i++)
{
stmt.str("");
stmt<<" update books set qty ="<< qty[i]<<"where id = "<<b_id[i]<<";";
query= stmt.str();
q= query.c_str();
mysql_query(conn,q);
}
cout<<" The order recieved has been updated .";
}
该错误在这两行中:
b_id[i]=(int)row[0];
qty[i]= (int)row[1];
我尝试使用 (long) 而不是 (int) ,期望它将我的 int 从 4 字节转换为 8 字节,但我仍然遇到相同的错误(从 'char*' 转换为 'int' 会丢失精度)
【问题讨论】:
-
您首先要通过将
char *转换为int来实现什么目的? -
你有没有想过为什么你需要这么多变量?
query = stmt.str(); q = query.c_str(); mysql_query(conn, q);可以替换为mysql_query(conn, stmt.str().c_str());并且两个变量和两行代码已被删除。调用函数时不必使用变量,可以使用表达也。 -
row的类型是什么? (我没有看到它的声明。)
标签: c++