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

标签 c

我正在编写一个代码,我想在其中迭代字符串数组。但有时我可能只需要迭代一个字符串,而该字符串的形式为 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/

相关文章:

c - 在 C 中,追加到打开的文件以供 Windows 控制台和 Linux 上的另一个程序读取

c - 当输入 y 时,循环将跳过宏 do while 循环

c++ - 如何从 C 文件调用 C++ 构造函数

c - 如何使用 printf 系列可移植地打印 size_t 变量?

c - 为什么 isnormal() 说一个值是正常的,而实际上不是?

c - 错误 : ',' expected (got "10") in C programming

c - 指向指针参数变量的指针以及它们如何工作?例如功能(int ** ptr)

c - "Illegal hardware instruction"来自非常简单的代码

c - 字母字符打印错误的 ISO 代码

c - 为什么我们不能像c中的指针一样增加数组呢?