如何在C中找到argv []的长度 [英] How to find the length of argv[] in C
问题描述
#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
}
}
我得到的错误是关于第7行的.它说从不兼容的指针类型传递'strlen'的参数1". strcat
函数出现相同的错误.
对于两个函数,它也都说给出了char **
但期望是const char *
".
The error I'm getting is for line 7. It says "passing argument 1 of 'strlen' from incompatible pointer type". I get the same error for the strcat
function.
It also says "given a char **
but expected a const char *
" for both functions.
我试图用argv
的所有元素(第一个元素除外)填充一个新数组.我尝试了argv = &argv[1]
,但没有成功.
I'm trying to populate a new array with all the elements of argv
except the first. I tried argv = &argv[1]
and it did not work.
strlen()
和strcat()
函数不使用char
数组吗?
Do the strlen()
and strcat()
functions not take char
arrays?
推荐答案
int main(int argc, char *argv[])
argv
是指向char
的指针的数组(即字符串数组).该数组的长度存储在argc
参数中.
argv
is an array of pointers to char
(i.e. array of strings). The length of this array is stored in argc
argument.
strlen
用于检索必须为空终止的单个字符串的长度,否则行为是不确定的.
strlen
is meant to be used to retrieve the length of the single string that must be null-terminated else the behavior is undefined.
这篇关于如何在C中找到argv []的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!