2

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

MSalters
  • 159,923
  • 8
  • 140
  • 320
Pepsi_1
  • 121
  • 2
  • 12

5 Answers5

4

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.

Community
  • 1
  • 1
4pie0
  • 27,469
  • 7
  • 70
  • 110
  • 1
    Is there another way of doing the same thing? Like declare **ptr and pass it as &ptr? – Pepsi_1 Sep 27 '14 at 19:41
  • @Pepsi_1 yes, exactly as you said, explanation added – 4pie0 Sep 27 '14 at 19:41
  • So is the programmer using this * & syntax so he doesn't have to deference the pointer inside the function while checking the pointer for different characters like %, ' ', '*' and so on? I'm still trying to wrap my head around why someone would want to do this. – Pepsi_1 Sep 27 '14 at 19:52
  • @Pepsi_1 yes, this might be the case, but to fully answer your question you would have to paste Function() code – 4pie0 Sep 27 '14 at 19:55
3

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*)

Ben Voigt
  • 260,885
  • 36
  • 380
  • 671
1
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

Columbo
  • 57,033
  • 7
  • 145
  • 194
0

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.

MicroVirus
  • 5,191
  • 2
  • 25
  • 47
0

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 */
}


Other Notes:
  • Passing an argument by reference is not supported in the C language; this function prototype is valid for C++, but not for C.
  • It is considered a good idea to put the name of each arguments (not just the data type) in the function prototype:

    Put name of parameters in C function prototypes?

Community
  • 1
  • 1
A B
  • 3,824
  • 1
  • 18
  • 23