对于这个问题的标题,我事先表示歉意,但是似乎没有比这更好的了。
这里的想法是argv在另一个变量中复制,本质上是复制它。因此,函数功能的基本思想是,malloc()用于为副本请求一些空间,然后遍历argv每个元素的副本进行迭代。
这是我正在使用的代码,开发环境现在是Visual Studio 2019(即使严格来讲不是C编译器...):
// Returns a copy of an array of strings (intended for argv, but should work with any of them):
wchar_t** copyArgv(size_t argc, wchar_t* argv[]) {
// Allocate space for the array of arguments:
wchar_t** argsCopy = malloc(((argc + 1) * sizeof(wchar_t*)));
if (!argsCopy)
return NULL;
// Copy each one of them:
for (size_t i = 0; i < argc; i++) {
argsCopy[i] = _wcsdup(argv[i]);
if (!argsCopy[i]) {
// Should …Run Code Online (Sandbox Code Playgroud)