Reputation: 121
I have a function here at work and I'm trying to wrap my head around it.
Can someone explain what this code is doing?
unsigned int Function( BYTE *&, BYTE );
I have never seen a parameter with *&
. It is used on multiple functions. I've tried looking in books for answers and looking online. I can't find any information on this format and what it is doing.
Any help I would appreciate it.
Now I have seen functions with the use of *
and &
but never together like above.
Also, I know the return value is unsigned int
. :)
I'm mostly just asking about the one parameter.
Thanks
Upvotes: 2
Views: 3627
Reputation: 29724
In declaration
unsigned int Function( BYTE *&, BYTE );
the *&
stands for pass pointer by reference, what means that changes made to pointer inside this function will be made to original pointer and not to its copy. You would want to pass a pointer by reference if you have a need to modify the pointer rather than the object that the pointer is pointing to.
@FredOverflow's example follows:
template< typename T>
void paranoid_delete( T *&p)
{
delete p;
p = nullptr;
}
You can also pass a pointer by pointer which can look like this:
void freeAndZero( void** ptr)
{
free( *ptr);
*ptr = 0;
}
Without the reference, you would only be changing a local copy of the pointer, not affecting the caller.
Upvotes: 4
Reputation: 4148
Whether the first parameter is BYTE *
, or BYTE * &
, you still pass a pointer of type BYTE *
as the first argument from the caller.
The ampersand in the first parameter allows the Function
function to change the address the pointer refers to. Any modification of the first parameter changes the address of the first parameter from the point of view of the calling function.
If the first parameter's type had been BYTE*
rather than BYTE *&
, the parameter would also be the address at which a BYTE
was stored.
unsigned int Function( BYTE *&, BYTE );
This function can be invoked as follows:
int main(int argc, char *argv[]) {
BYTE b;
BYTE* ptrToByte;
unsigned int FunctionResult;
b = 22;
ptrToByte = &b;
FunctionResult = Function(ptrToByte, b);
/* At this point, ptrToByte may or may not have */
/* changed, depending on what lines of code the Function function */
/* executed */
}
Upvotes: 0
Reputation: 60989
unsigned int Function( BYTE *&, BYTE );
The first parameter is of type "reference to pointer to BYTE
". Read it from right to left, it will be easier to understand.
BYTE * &
^ ^ ^
| | |------ Reference to
| |------- Pointer to
|----------- BYTE (duh).
You can also use cdecl and substitute BYTE
by char
and Function
by any letter in the declaration:
declare f as function (reference to pointer to char, char) returning unsigned
Upvotes: 1
Reputation: 283684
Composing simple things to make complex systems is probably the most important concept in programming. Let's break down what is happening here.
You know that if you see BYTE*
, that type is "pointer to BYTE".
Now if you see T&
, you know that is "reference to T".
When these get combined, making T
equal to BYTE*
, you get (BYTE*)&
. Due to the grammar grouping rules, BYTE*&
means the same thing. And the meaning remains "reference to something... and something is pointer to BYTE".
(note that grammar grouping rules are important... const T
with T
of BYTE*
is written as const (BYTE*)
or BYTE* const
, but not the same as const BYTE*
)
Upvotes: 3
Reputation: 5477
Parse the parameter expression from right to left: BYTE * &
is a reference to a BYTE *
. Right-to-left parsing is often useful with pointers and references.
Upvotes: 0