Why is TCHAR(&name)[100] passed to the function instead of TCHAR *name ?

 void CreateName(TCHAR(&name)[100], int number) { _stprintf_s(name, TEXT("Your number: %d"), number); } 

    1 answer 1

    In the Microsoft documentation, the _stprintf_s function is the following macro definition

     #define _stprintf_s swprintf_s 

    In turn, the function swprintf_s is defined as

     template <size_t size> int swprintf_s( wchar_t (&buffer)[size], const wchar_t *format, ... ); // C++ only 

    That is, it is a template function whose template parameter is the size of the array. Therefore, the array used as an argument is passed to the function by reference so that the function can determine the size of the array.

    Accordingly, the CreateName function CreateName declared its first parameter as a link to an array, which it then passes to the _stprintf_s function. If she did not accept the array by reference, she would not be able to call the _stprintf_s function.

    To call the CreateName function CreateName you just need to specify the name of the array as an argument, as the function itself does in turn when calling the _stprintf_s function.