return array (char, double) from function

Code below is part of the file i have created to return result of mysql database. it works ok by it self when i print the result (check comment "print 1") but i would like to transfer this array to somewhere else. so would like this function to return me an array filled with the result some how but its not working at all
compile error: cannot convert 'double' to 'double' in return
i think i am doing very basic mistake here somewhere but cant find it
please help.

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
double Querydb(char *query)
{    int i=0;
    char buffer[50];
    double value1, Myarray[30000];
    if (mysql_query(conn, query)) {
    fprintf(stderr, "%s\n", mysql_error(conn));
    }
    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[1]);    //print 1
        sprintf(buffer,"%s",row[1]);
        value1 = atof(buffer);
        Myarray[i]=value1;
        printf("Myarray = %f\n",Myarray[i]);
        i++;
        printf("ivalue = %d\n",i);
        }

    //close connection

    mysql_free_result(res);
    return Myarray; //doesnt work ??????
}
Firstly, Myarray is local var which means it is sitting at stack. After Querydb() exits, the stack will be returned. So Myarray won't exist any more.

If you don't mind, you can pass a ponter or reference of an array from the calling function to Querydb(). I prefer this method.
thanks for the reply b2ee. but could you please put some code in so that it can explain bit more. do you mean pass on query to Querydb? the other thing i tried was to use Querydb as an array.
 
Char **Querydb (char *query) 

but then i had trouble inserting result comming out from row to Querydb
Last edited on
The most direct way is:
"
double Querydb(char *query, double arr[]);
// The other way is the below, and both way should work.
//double Querydb(char *query, double* arr);
{
....
}
"
When calling:
"
double Myarray[30000];
Querydb(query, Myarray);
"
Make sure you know the elements in row wouldn't exceed 30000.

The more suitable way is to use "vector" which is the dynamic array in C++.

Another concern, put the size 30000 arry in stack is not good habit. May be for the moderPC, there is no issue for that. Stack overflow is an obvious risk.
received compile error:
cannot convert 'double*' to 'double' in return

my .h file is having
 
extern double Querydb(char *query, double *Myarray);


my .cpp file is having same code as above except now its:

ouble Querydb(char *query, double Myarray[30000])

and i'm calling in main:
1
2
double Myarray[30000];
Querydb("select * from table1;", 0);


the error is at the line where its returning Myarray
thanks again
Where is "0" from at "Querydb("select * from table1;", 0);", "Myarray" should be there.


b2ee
Appreciate your help

.h file
 
extern double Querydb(char *query, double *Myarray);


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

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


in main
1
2
double Myarray[30000];
Querydb("select * from meter where Num between '28292' and '28420';",Myarray);


compile error code:

cannot convert 'double*' to 'double' in return
#22 in .cpp is wrong. You don't need return anything now. The Querydb() is double type, and Myarrary is a "double*".

Just delete #22, and change Querydb() type to "void". Try again.

B2ee
Last edited on
hurraaaaayyyyyyyyyyyyyyy!!!

yes i found the same at this link

http://www.dreamincode.net/forums/topic/88639-cannot-convert-from-double-300-to-double/

that was great help thanks a lot

THanKs
Topic archived. No new replies allowed.