warning: initialization makes integer from pointer without cast [-Wint-conversion]

1

I have the following code in C that makes a connection to a mysql database and performs a query:

#include <mysql.h>
#include <stdio.h>

int main(void){

    MYSQL Connection;
    MYSQL_RES *res;
    MYSQL_ROW row;

    mysql_init(&Connection);

    if(mysql_real_connect(&Connection, "127.0.0.1", "root", "xururuca", "test", 0, NULL, 0)){

        printf("Connection: Ok...\n\n");

    }else{

        printf("Error in connection\n\n");
    }

    mysql_query(&Connection ,"select id from accounts where username='Alan' and password='lixodoesgotogostoso';");

    res=mysql_store_result(&Connection);

    row=mysql_fetch_row(res);

    printf("%s\n", row[0]); //ele imprime 3 (Ok...)

    int userID=row[0]; //Agora tento passar esse 3 para uma variável do tipo int...mais não rola

    printf("%d\n", userID); //Imprime lixo e não 3 que seria o correto

    mysql_free_result(res);
    mysql_close(&Connection);

    return 0;
}

When compiling: warning: initialization makes integer from pointer without cast [-Wint-conversion] |

So, I want my 'result set' to go to the variable userID, but how do I do it without major problems?

    
asked by anonymous 02.03.2018 / 01:58

1 answer

0

Note that printf works but the formatter is %s . This means that the value that has row[0] is actually a string , or being more exact, a pointer to an array of characters. Since it is a pointer the compiler complains because it is saving it in int .

You can also confirm in the documentation that MYSQL_ROW is an array of strings

To convert the string value to integer you can use the atoi function:

int userID = atoi(row[0]);
    
02.03.2018 / 02:41