[英]C++ pass by reference WCHAR** into a function
我在將代碼傳遞給函數的WCHAR**
引用中編寫了以下代碼,以便將其歸檔在函數中。 在函數內部,我正在填充此二維數組。 我正在嘗試使用引用傳遞來完成此操作。
BOOL get_file_names(WCHAR** outname)
{
WCHAR **outhandlername = NULL;
if (get_all_the_file_handelers( &outhandlername) > 0)
{
(*outname)[0] = *outhandlername[0];
}
return ret;
}
int get_all_the_file_handelers(WCHAR** outhandleName )
{
for (i = 0; i < 10; i++)
{
WCHAR *handleName = get_handle_name(handle, i);
outhandleName = (WCHAR**)realloc(outhandleName, sizeof(WCHAR)*(10 + 1));
(*outhandleName) = (WCHAR*)realloc(*outhandleName, sizeof(WCHAR)*(1024));
*outhandleName = handleName;
}
return 0;
}
但這似乎不起作用,有人可以幫助我了解在這種情況下按引用傳遞對WCHAR
數組的工作方式。 當我有WHCAR**
,將&WCHAR
傳遞給第二個函數是正確的,並且在第二個函數內部我應該如何為二維WCHAR
數組分配值
int _tmain(int argc, _TCHAR* argv[])
{
WCHAR *outhandlename=NULL;
get_file_names(&outhandlename);
DBGLOG("handler name %ls", outhandlename);
return 0;
}
BOOL process_search_file_handle( WCHAR** str)
{
*str = (WCHAR*) malloc(1024);
*str = L"TestName";
return true;
}
BOOL get_file_names(WCHAR** outname)
{
WCHAR **outhandlername = NULL;
if (get_all_the_file_handelers( &outhandlername) > 0)
{
*outname = outhandlername[0];
DBGLOG("outhandlername value %ls", outhandlername[0]);
DBGLOG("str value %ls", *outname);
}
return true;
}
將outHandleName設置為realloc的輸出后,您已經覆蓋了outHandleName,因此您將無法從調用函數中更改outHandleName的值。
你可以說:
*outHandleName = (WCHAR**) realloc(...);
您還必須將get_all_the_file_handelers
的方法標頭get_all_the_file_handelers
為:
int get_all_the_file_handelers(WCHAR *** outHandleName)
這是因為在此方法中使用的是指向雙指針的指針。
另外,您不是通過引用傳遞的,而是通過指針傳遞的。
另外,您不應該在這里使用realloc,因為您正在為數組進行初始分配。 應該在循環之前分配10個元素的數組,如下所示:
*outHandleName = (WCHAR **)malloc(sizeof(WCHAR *)*(10+1));
(*outHandleName)[10] = NULL;
請注意,空分配-您要在10個項目的數組中為11個項目分配空間,因此我假設您將最后一個項目用作占位符-作為NULL-來標記數組的結尾。
最后,您不需要為10個句柄的每一個分配字符串緩沖區的空間,因為您可以從get_handle_name返回一個字符串。
作為獎勵,即使您的其余代碼表明您沒有回報,您也不會返回任何東西。
最終方法將是:
int get_all_the_file_handelers(WCHAR ***outHandleName) {
*outHandleName = (WCHAR **)malloc(sizeof(WCHAR *)*11);
(*outHandleName)[10] = NULL;
for(int i = 0; i < 10; i++) {
WCHAR *handleName = get_handle_name(handle, i);
(*outHandleName)[i] = handleName;
}
return .... /* WHAT AM I RETURNING?? */ ;
}
這是一些使用標准庫生成寬字符串列表的示例代碼。 您應該稍后學習如何處理指針數組,但是我建議您首先學習如何使用STL。
#include <cstdlib>
#include <experimental/filesystem>
#include <iostream>
#include <locale>
#include <string>
#include <vector>
#if _WIN32 || _WIN64
// Windows needs a little non-standard magic for this to work.
#include <io.h>
#include <fcntl.h>
#include <locale.h>
#endif
using std::endl;
using std::size_t;
using std::wcout;
using std::experimental::filesystem::path;
void init_locale(void)
// Does magic so that wcout can work.
{
#if _WIN32 || _WIN64
// Windows needs a little non-standard magic.
constexpr char cp_utf16le[] = ".1200";
setlocale( LC_ALL, cp_utf16le );
_setmode( _fileno(stdout), _O_U16TEXT );
#else
// The correct locale name may vary by OS, e.g., "en_US.utf8".
constexpr char locale_name[] = "";
std::locale::global(std::locale(locale_name));
std::wcout.imbue(std::locale());
#endif
}
std::vector<std::wstring> files_in_wd()
// Returns a list of filenames in the current working directory.
{
const path cwd = std::experimental::filesystem::current_path();
std::vector<std::wstring> filenames;
for ( const path& file : cwd )
filenames.emplace_back( file.filename().wstring() );
return filenames;
}
int main(void)
{
init_locale();
const std::vector<std::wstring> filenames = files_in_wd();
for ( const std::wstring& ws : filenames )
wcout << ws << endl;
return EXIT_SUCCESS;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.