I am a c# developer learning/relearning/brushing up on c++
I’m working on database access I have the following code and im having trouble understand what the & does in this case.
SQLHENV hEnv = NULL;
if (SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv) == SQL_ERROR)
{
If I remove the & I get this error.
'SQLAllocHandle' : cannot convert parameter 3 from 'SQLHENV' to 'SQLHANDLE *'
at first I thought it was simply passing this field in as a reference but based on the error it reads more like it is some how allowing it to convert?
In the context of your current example, the
&operator is used to obtain the address of a variable.From the error message, it looks like
SQLAllocHandleexpects the parameter 3 to be of typeSQLHANDLE *(pointer toSQLHANDLE).When you “removed” the
&, you passed aSQLHENVto it, which could not be converted to the required type. But when you did “include” the&, it worked because I am guessingSQLHANDLEistypedefed toSQLHENV, or aSQLHENV*is convertible toSQLHANDLE*due to inheritance or other reason.I need to do guesswork because the signatures of
SQLAllocHandleand the class relation ofSQLHENVandSQLHANDLEis not mentioned in the question.