When I was toying around with some of the provided code from an online site, I have rather encountered a bizarre glitch I have never expected: a variable suddenly changes its value without any manual assignment.
here is the code below:
int rc = fwrite(conn->db, sizeof(struct Database), 1, conn->file);
printf("rc is equal to %d\n", rc); // should print out 1
if (rc != 1) die("Failed to write Database.");
rc = fflush(conn->file);
printf("rc is equal to %d\n", rc); // should print out 0
if (rc == -1); die("Cannot flush database"); // error handling
// error comes up because rc suddenly changes to -1
I do not understand how it happens, but like to know why a variable suddenly changes in C when it is not supposed to.
code source:
http://c.learncodethehardway.org/book/learn-c-the-hard-waych18.html
(Heap Stack and Memory under Database_write)
By the way, I am using vim in Terminal on Mac osx 10.6 snow leopard.
It does not change to -1. It is still zero.
You have a semicolon (;) in your code where you should not have it.
You should have instead: