Jump to content
  • Advertisement
Sign in to follow this  

C++ (int) cast giving unexpected results

This topic is 4261 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

cout<<row[3] This line prints out: 20 cout<<(int)row[3] This line prints out: 6499321 Can anyone help explain why? Row is declared as: MYSQL_ROW row; And row contains: row = mysql_fetch_row(res_set) Any help is appreciated!

Share this post


Link to post
Share on other sites
Advertisement
From the (rather vague) mySQL documentation:

Quote:
MYSQL_ROW
This is a type-safe representation of one row of data. It is currently implemented as an array of counted byte strings. (You cannot treat these as null-terminated strings if field values may contain binary data, because such values may contain null bytes internally.)


Think of MYSQL_ROW as a char**, and treat it as such. The reason your first cout works is because row[3] is a null terminated char* containing '2','0', and NULL.

The exception to this if the field type is a binary blob, or whatever they call it in MYSQL. For your example you probably care more about storing the data in a useful format, in C the correct way would be atoi(). eg:
int i = atoi(row[3]); // determine the interger value represented by the string
cout << i; // print the interger using cout


More on atoi: http://www.cplusplus.com/ref/cstdlib/atoi.html

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!