如何在C中找到argv []的长度

use*_*351 15 c strlen argv strcat

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]){
    int fir; //badly named loop variable
    char *input[] = calloc( strlen(argv), sizeof(char)); //initializing an array
    for( fir = 1; fir< strlen(argv); fir++){ //removing the first element of argv
        strcat(input, argv[fir]); // appending to input
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是第7行.它说"从不兼容的指针类型传递'strlen'的参数1".我得到了相同的错误strcat功能.对于这两种功能,它也说"给出了一个char **但是预期的const char *".

我正在尝试使用argv除第一个之外的所有元素填充新数组.我试过了argv = &argv[1],但没用.

这些strlen()strcat()函数不采用char数组吗?

Lih*_*ihO 31

int main(int argc, char *argv[])
Run Code Online (Sandbox Code Playgroud)

argv是一个指针数组char(即字符串数组).该数组的长度存储在argc参数中.

strlen 用于检索必须为空终止的单个字符串的长度,否则行为未定义.


Jac*_*man 6

不确定为什么没有人建议将strlen更改为指向char指针数组中的特定条目吗?

 strlen(argv[0])     // also, 1, 2, up to (argc - 1)
Run Code Online (Sandbox Code Playgroud)

另外,http: //www.cdecl.org/有助于确认该char *argv[]声明是:declare argv as array of pointer to char