c - 在 C 中传递数组的一部分

标签 c arrays string

如果我有一个 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/

相关文章:

Android 字符串数组到数组

java - 将 String[] 更改为 Vector<String>

c - 使用带双指针的 malloc 时出现段错误

c - 如何在每 1000 个值后停止无限循环?

javascript - 使用正则表达式获取字符串

java - (Java) 将数字字符串转换为整数数组

c++ - 采访 : Adding two binary numbers given as strings

python - 从python中的字符串中去除不可打印的字符

c++ - 有没有办法用C++制作一个运行在Java手机上的程序?

c - 是否可以将 page_size 存储到具有静态存储持续时间的对象中?