Warning: initialization makes integer from Pointer without a cast [-Wint-Conversion]

Asked

Viewed 1,916 times

1

I have the following C code that connects 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 a cast [-Wint-Conversion]|

So, I want my 'result set' to go to the userid variable, more as I do it without major problems?

1 answer

0


Note that the printf works but the formatter is %s. This means that the value it has in 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 as it is storing it in a int.

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

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

int userID = atoi(row[0]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.