deprecated conversion from string const to char*

I am having an error after compiling the program with above warning. i believe i'm doing something wrong my code is as below. program terminates with seg fault

in main i'm calling sqlfunction.cpp to query mysql database.
timer loop dumps the result to the mysql database by querying sqlfunction.
program runs in the main ok even with the warnings. but it doesnt run in the timer loop and terminates with seg fault.?????

in main cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
void *timer(void *arg)
{

        int ret;
        char command[1024];
        double Myarray[3000];

        while (1){
        clock_t endwait;
        endwait = clock() + 1 * CLOCKS_PER_SEC;
        while (clock() < endwait){}//do nothing

        float mul=0;
        mul= GetNemo(0);
        mul= MyFloat(mul);
        printf("timed float ===== %f\n",mul);

        int i = 0;
        sprintf(command,"insert into meter (Voltage) values (%f);",mul); //warnign here
        ret = Querydb(command,Myarray);
        printf("Querydb = %d\n",ret);
        printf("__________\n");
             }

        return arg;
}
int main(int ac, char **av)
{
PARAM p;

int s, ret;

ret = Opendb("localhost","root","pass","tetsdB"); //warning nere
printf("opendb = %d\n",ret);

ret = thread.create(timer,NULL); //THREAD FOR TIMER
printf("thread_timer ret = %d\n", ret);

MYSQL_RES *res;
double Myarray[128];
int i = 0;
char command[1024];

ret = Querydb("insert into table (Voltage) values (10);",Myarray);
if(ret != 0){
    for(i=0;i<128;i++){
        printf("myarray ==== %f\n",Myarray[i]);}
    mysql_free_result(res);
       }


.h file

1
2
3
4
extern bool Opendb(char *pc, char *user, char *pass, char *db);
extern bool Closedb();
extern bool Querydb(char *query, double *Myarray);
extern int GetNemo(int bytes);


in sqlfuntion.cpp one of the function is..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
bool Querydb(char *query, double Myarray[3000])
{
    int i=0;
    char buffer[50];
    double value1;
     if (mysql_query(conn, query)) {
    fprintf(stderr, "%s\n", mysql_error(conn));
    return 0;
    }

    else {
    res = mysql_use_result(conn);
    //output table name
    printf("MySQL Tables in mysql database:\n");
    while ((row = mysql_fetch_row(res)) != NULL){
        printf("%s\n",row[0]);
        sprintf(buffer,"%s",row[1]);
       value1 = atof(buffer);
        Myarray[i]=value1;
        i++;
        }
    return 0;
    }
 }

Last edited on
Some of your functions take char*, so you can't pass them a string literal (in double quotes) legally. If that string it modified ever, that could cause an error.
thanks Zhuge for quick reply, i understand the why... but dont get it how do i change it

for example i have to pass the query as below
1
2
        sprintf(command,"insert into meter (Voltage) values (%f);",mul); //warnign here
        ret = Querydb(command,Myarray);


cause my "mul" is part of modbus daemon so is comming from outside and then i need to put that value of "mul" in to the mysql tables

my query for the mysql will always going to be a string ("select * from table") which must be passed on to function for execution somehow
Last edited on
Topic archived. No new replies allowed.