如果我有一个 char 数组 char *str[] = {"qwe", "asd", ..., "hello", "there","pal"};
我如何将那个具有特定范围的数组传递给一个函数(特别是 execv),比如只传递“asd”直到“hello”?
我知道您可以传入类似 str+1 的内容来跳过第一个。这可能吗?
最佳答案
如联机帮助页所述,您不能为 execv() 执行此操作:
The list of arguments must be terminated by a null pointer, and, since these are
variadic functions, this pointer must be cast (char *) NULL.
execv()中str的用法如下例
void func1(char *str[])
{
for(int i=0; str[i]!=NULL; i++)
printf("%s:%s\n", __func__, str[i]);
}
但是,如果函数有这样的声明和用法:
void func2(char *str[], int n)
{
for(int i=0; i<n; i++)
printf("%s:%s\n", __func__, str[i]);
}
你可以像下面这样调用它,只传入“asd”直到“hello”。
func2(str+a, n);
//where str[a] is "asd" and str[a+n-1] is "hello"
关于c - 在 C 中传递数组的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54799617/