将变量添加到 char *

Adding variables to char *

我想将我的 char * 中的值更改为声明的 int 和 double,这样我就可以在不触及 char *sql 的情况下更改它们。 (在 ubuntu 使用 C)

我该怎么做?

代码:

int sensor_id = 10;

double temp = 22.22;

char *sql = "INSERT INTO test_table(sensor_id, sensor_value) VALUES(10, 22.22)";

试试这个(脏,没有错误检查,可能会发生缓冲区溢出):

const char *sqlformat = 
            "INSERT INTO test_table(sensor_id, sensor_value) VALUES(%d, %f)";

char sql[200];
sprintf(sql, sensor_id, temp);

现在你在 sql 缓冲区中有了你想要的东西。

这是很基础的C的东西,建议你学习C。