gpt4 book ai didi

c - WCHAR [256] as WCHAR ** 即,迭代数组字符串与仅迭代一个字符串

转载 作者:行者123 更新时间:2023-11-30 19:26:27 27 4
gpt4 key购买 nike

我正在编写一个代码,我想在其中迭代字符串数组。但有时我可能只需要迭代一个字符串,而该字符串的形式为 WCHAR[256] 现在问题是如何将 WCHAR[256] 视为 LPCWSTR* 以便我不必处理 WCHAR[256]作为迭代时的特殊情况。下面是代码示例。

请告诉我这里是否还有其他优雅的解决方案?

WCHAR BranchName[MAX_PATH];
BOOLEAN AllBranches;

WCHAR* DefaultBranches[] = {
L"branch1",
L"branch2",
L"branch3",
};

INT
wmain (
_In_ INT Argc,
_In_ WCHAR **Argv
)
{

WCHAR **Branch; // Pointer to array of strings. How can I make this point to WCHAR[256]?
UINT Count;
UINT Index;

ParseArguments(Argc, Argv); // Set AllBranches to true if Custom branch is not provided

if (AllBranches) { // If all branches is true
Branch = DefaultBranches;
Count = ARRAYSIZE(DefaultBranches);
} else { // BranchName containing the custom branch name provided as cmd line arg
// How can BranchName be assigned to Branch pointer so that iterating
// over just one string do not become a special case?
Branch = ??? <-- NOT SURE HOW TO DO IT!!!
Count = 1;
}

// I want this loop to iterate over array string pointers and also with
// one string
for (Index = 0; Index < Count; Index += 1) {
// do some thing with *Branch and I don't want to duplicate it
// when iterating over array of strings vs iterating over a string
Branch++;
}

return 0;
}

最佳答案

要么

WCHAR * BranchNameArr [] = { BranchName };
Branch = BranchNameArr;

WCHAR * BranchNamePtr =  BranchName;
Branch = & BranchNamePtr;

还好。

关于c - WCHAR [256] as WCHAR ** 即,迭代数组字符串与仅迭代一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57237520/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com