0

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?

Chowlett
  • 45,935
  • 20
  • 116
  • 150
Paul Wade
  • 591
  • 5
  • 17
  • 1
    Why do you want to remove the address operator `&`? The function requires a pointer to a SQLHENV. – harper Jan 04 '13 at 16:29
  • I don't I was just trying to learn from the error why it was there in the first place. – Paul Wade Jan 04 '13 at 16:35
  • Don't hesitate writing the steps that you've done in that order you've done. This can help to understand the question better. – harper Jan 04 '13 at 16:39

4 Answers4

6

When appearing as a unary operator in an expression, & is the address-of operator. The code you're showing passes a pointer to hEnv into SQLAllocHandle().

You might want to pick up a good C++ book.

Community
  • 1
  • 1
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
5

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 SQLAllocHandle expects the parameter 3 to be of type SQLHANDLE * (pointer to SQLHANDLE).

When you "removed" the &, you passed a SQLHENV to it, which could not be converted to the required type. But when you did "include" the &, it worked because I am guessing SQLHANDLE is typedefed to SQLHENV, or a SQLHENV* is convertible to SQLHANDLE* due to inheritance or other reason.

I need to do guesswork because the signatures of SQLAllocHandle and the class relation of SQLHENV and SQLHANDLE is not mentioned in the question.

Masked Man
  • 1
  • 7
  • 40
  • 80
0

& is the address operator. It's taking the local hEnv and passing the address of it. Now you have not the type but a pointer to the type you are referring.

so

void foo()
{
     int i = 1; 
     int * iptr = &i; 
} 

When you skip the address operator you still have the type, not the pointer to the type.

harper
  • 13,345
  • 8
  • 56
  • 105
rerun
  • 25,014
  • 6
  • 48
  • 78
-4

& does mean to pass by reference. The compiler is just telling you it can't convert from a reference to value type

EDIT: When a function accepts an ADDRESS(pointer), it is called pass by reference. I'm sorry if my answer confused some people, but in this case your function is expecting a REFERENCE to a variable, so you need to pass it the ADDRESS of that variable.

Kyle Preiksa
  • 516
  • 2
  • 7
  • 23
  • 1
    -1: In this case, it's not passing by reference, it's taking the address. – Angew is no longer proud of SO Jan 04 '13 at 16:15
  • 1
    `typename&` may be a reference, but `&variable` is taking an address. – Chowlett Jan 04 '13 at 16:15
  • In C++, "pass by reference" means declare the parameter type as a reference. If the parameter type is a pointer, it is a pointer passed by value. – Angew is no longer proud of SO Jan 04 '13 at 16:31
  • So, effectively, you are reversing the roles of what I said? I was saying that if you expect a pointer you need to pass an address, but what the OP's scenario is is expecting an address, so you have to pass it a pointer? Effectively, both ways pass a memory location as the parameter though... – Kyle Preiksa Jan 04 '13 at 16:35
  • the answer might be wrong but the explanations of why actually go further than just the answer to helping understand the difference.Since I can now see the difference between address and by ref. – Paul Wade Jan 04 '13 at 16:38