Reputation: 1103
I'm writting a function to open a database, this is how it looks so far :
bool
Sqlite3::openDB(std::string filename, sqlite3* db)
{
std::ifstream file ( filename.c_str() );
if( !file )
{
std::cerr << "Can't open database, file not found. " << std::endl;
return false;
}
file.close();
if( sqlite3_open(filename.c_str(), &db) )
{
std::cerr << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return false;
}
std::cout << (int)db << std::endl; // added to check the pointer (3)
return true;
}
in main() :
int main(int argc, char **argv)
{
sqlite3 *db = nullptr;
std::cout << (int)db << std::endl; // (1)
// Open Database
sqlite3_engine.openDB("./bedrock.db",db);
std::cout << (int)db << std::endl; // (2)
/*...*/
}
at (1) I get null, at (3) I get something not null but at (2) I get something null and I don't understand why...
Upvotes: 0
Views: 145
Reputation: 152817
Function parameters are passed by value. openDB()
gets a copy of the db
variable, modifies the copy and prints it. The original variable is not modified.
If you want to modify the parameter, pass by reference (e.g. as sqlite3 *&
).
Upvotes: 1