0

I am having issues getting a function to run and having the results of a MySQL query to be saved as a variable that other functions can use and call upon. I know the results get read from the table as a string. I was able to do this fine when getting the results and converting it to a float and then passing the results to a pointer. But I can not seem to figure out how to get the results as a string, and compare it with another string to see if they match or do not. No matter what I have tried to do, I can not seem to get a value to be saved as a string to a variable outside the function.

Here is the code of how I got it to work as a float:

(Outside the main function)

float temperature_reading;
float *p_temperature_reading= &temperature_reading;
float humidity_reading;
float *p_humidity_reading= &humidity_reading;

The function I have working with the float, that I can save to a global variable

void MIA_get_current_temperature()
{

    const char *query = "SELECT Temperature, Humidity FROM `temperature` WHERE Type='Current_Temperature'";

    if (mysql_query(conn, query) != 0)
    {
        fprintf(stderr, "%s\n", mysql_error(conn));
        exit(-1);
    } else {
        MYSQL_RES *query_results = mysql_store_result(conn);
        if (query_results) 
        { // make sure there *are* results..
            MYSQL_ROW row;

            while((row = mysql_fetch_row(query_results)) !=0)
            {

                float f = row[0] ? atof(row[0]) : 0.0f;
                float h = row[1] ? atof(row[1]) : 0.0f;

                *p_temperature_reading = f;
                *p_humidity_reading = h;

                printf("The Temp & Hum from DB is: %.1f & %.1f\n", *p_temperature_reading,*p_humidity_reading);    
            }

        /* Free results when done */
        mysql_free_result(query_results);
        }
    }
}

This is the function I can not get to work:

(Outside main Function)

 char ac_mode[256];
 char *p_ac_mode = &ac_mode[256];

Function:

void MIA_get_desired_temperature()
 {

    const char *query = "SELECT Mode, Desired_Temperature, Threshold FROM `ac_mode` WHERE Status='ON'";

    if (mysql_query(conn, query) != 0)
    {
        fprintf(stderr, "%s\n", mysql_error(conn));
        exit(-1);
    } else {
        MYSQL_RES *query_results = mysql_store_result(conn);
        if (query_results) 
        { // make sure there *are* results..
            MYSQL_ROW row;

            while((row = mysql_fetch_row(query_results)) !=0)
            {

                char *ac = row[0] ? row[0] : "NULL";
                float desired_temperature = row[1] ? atof(row[1]) : 0.0f;
                int threshold = row[2] ? atof(row[2]) : 0.0f;

                *p_ac_mode = *ac;
                *p_desired_temperature = desired_temperature;  
                *p_threshold=threshold;

            }

        /* Free results when done */
        mysql_free_result(query_results);
        }
    }
}

char *ac is where I want the string to be stored.

Vlad
  • 145
  • 6
  • 19
  • "No matter what I have tried..". Please show what you have tried for the string version. Without that we can't tell you what you might be doing wrong. – kaylum Jan 29 '16 at 00:02
  • @kaylum I edited the posted – Vlad Jan 29 '16 at 00:23

2 Answers2

1

You need to use strcpy in your MIA_get_desired_temperature function. Also, you don't need the pointer p_ac_mode. Just copy into ac_mode directly.

strcpy(ac_mode, ac);
Eddie
  • 427
  • 3
  • 8
1

This line:

char *p_ac_mode = &ac_mode[256];

..is incorrect. You're probably trying to declare a pointer to the array (or maybe to its contents)... what you're actually doing is declaring a char * that points at the first byte after the array ac_mode. The [256] here isn't indicating that ac_mode has 256 elements, it's indexing the array to get element 256 (which would be the 257th char in the array if it were big enough -- but it's not, so it's outside the array). You're then taking the address of that out-of-bounds char and assign it to p_ac_mode, so that p_ac_mode to points to it.

To point p_ac_mode at the array contents, you'd just use char *p_ac_mode = ac_mode; (which makes it a char * pointing at the first char in the array). To get a pointer to the array itself, you'd use char (*p_ac_mode)[256] = &ac_mode;, which makes it a pointer to a 256-element array of char. In any case there's no need for p_ac_mode at all, because you can access the array through ac_mode directly in all the same places, and the bare array name ac_mode will usually decay to a pointer to its first char anyway.


With this line:

                *p_ac_mode = *ac;

..you're copying the first char from string ac to the first char after ac_mode (because that's what p_ac_mode points to, as explained above). I suspect you're actually trying to assign the whole ac string's contents to ac_mode via p_ac_mode -- but that won't work for a few reasons.

An array is actually a block of memory holding a series of values of the same type. Although in many situations the array name will decay to a pointer (the address of the array's first element), the array itself is the block of memory and not the pointer. You can't just assign a pointer (new address) to the array, and array contents aren't automatically copied this way either. The contents need to be copied over element by element, or using a function that copies the contents.

What you need to do is copy the contents of the string from your query results into the ac_mode array with strcpy() or similar. Just changing this line:

                *p_ac_mode = *ac;

to this:

                strcpy(ac_mode, ac);

...would do that.

Dmitri
  • 9,175
  • 2
  • 27
  • 34
  • so I changed the like *p_ac_mode = *ac; to strcpy(ac_mode, ac);, but now I get the error " warning: implicit declaration of function 'strcpy' [-Wimplicit-function-declaration]" and "warning: incompatible implicit declaration of built-in function 'strcpy' [enabled by default]" – Vlad Jan 29 '16 at 19:36
  • also regarding char ac_mode[256]; char *p_ac_mode = &ac_mode[256]; I can just get ride of char *p_ac_mode, and also for the ac_mode[256], do I keep the [256]? I thought I had to assign a size to the array. – Vlad Jan 29 '16 at 19:39
  • 1
    @Vlad you need to add `#include ` near the top of your source file to get the `strcpy()` prototype. That should work and get rid of the "implicit declaration" warning. – Dmitri Jan 29 '16 at 19:41
  • @Vlad When you first declare `ac_mode`, you *do* use the `[256]` to give the array size... but only in the declaration -- after that, the brackets are for indexing and not specifying the size. – Dmitri Jan 29 '16 at 19:43
  • okay I totally forgot to include the string.h header file. but what about the variables: char ac_mode[256]; char *p_ac_mode = &ac_mode[256]? What do I need to set them outside the main function to? Since you said I can get ride of the pointer (p_ac_mode) – Vlad Jan 29 '16 at 19:45
  • @Vlad Not sure what you mean... you can just use the array name, `ac_mode`, as a `char *` to access the string. `ac_mode` by itself is (usually) equivalent to `&ac_mode[0]` -- it gives a pointer to the characters in the array... and since it's global you can use that anywhere in the file past where you declare it. – Dmitri Jan 29 '16 at 19:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102032/discussion-between-dmitri-and-vlad). – Dmitri Jan 29 '16 at 19:52
  • I was wonderng if you can check out my post at http://stackoverflow.com/questions/41904422/mysql-query-select-fields-where-time-value-in-table-is-equal-to-current-time/41946869#41946869 , ot open a chat. I have gotten better with c but i am having in interesting issue with C and MySql – Vlad Jan 30 '17 at 23:46