C语言 如何在C中找到argv[]的长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18649547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find the length of argv[] in C
提问by user1787351
#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
}
}
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 strcatfunction.
It also says "given a char **but expected a const char *" for both functions.
我得到的错误是第 7 行。它说“从不兼容的指针类型传递 'strlen' 的参数 1”。我得到了同样的strcat函数错误。它还对这两个函数说“给定一个char **但预期为一个const char *”。
I'm trying to populate a new array with all the elements of argvexcept the first. I tried argv = &argv[1]and it did not work.
我试图用argv除第一个之外的所有元素填充一个新数组。我试过了argv = &argv[1],但没有用。
Do the strlen()and strcat()functions not take chararrays?
请执行strlen()和strcat()功能用不了char数组?
回答by LihO
int main(int argc, char *argv[])
argvis an array of pointers to char(i.e. array of strings). The length of this array is stored in argcargument.
argv是一个指向char(即字符串数组)的指针数组。该数组的长度存储在argc参数中。
strlenis meant to be used to retrieve the length of the single string that must be null-terminated else the behavior is undefined.
strlen旨在用于检索必须以空字符结尾的单个字符串的长度,否则行为未定义。
回答by HymanCColeman
Not sure why no one has suggested changing strlen to refer to a specific entry in the array of pointers to char?
不知道为什么没有人建议更改 strlen 以引用指向 char 的指针数组中的特定条目?
strlen(argv[0]) // also, 1, 2, up to (argc - 1)
Also, http://www.cdecl.org/helps in confirming that the char *argv[]statement is: declare argv as array of pointer to char
此外,http://www.cdecl.org/ 有助于确认该char *argv[]声明是:declare argv as array of pointer to char
回答by Kidambi Manoj
int count = 0;
while(argv[++count] != NULL);
Now, count will have the length of argv
现在,count 将具有 argv 的长度
回答by user1787351
argvis an array of char, strlenonly takes strings. If you want to get the length of each argument in argv(which is what I was trying to do), you must iterate through it, accessing the elements like so argv[i][j]. Using the argument argv[i][j] != '\0'. If you just want the number of arguments use argc.
argv是一个字符数组,strlen只需要字符串。如果你想获得每个参数的长度argv(这是我试图做的),你必须遍历它,访问像 so 的元素argv[i][j]。使用参数argv[i][j] != '\0'。如果您只想要参数的数量,请使用argc.
回答by Mohamad Ali Baydoun
argvis an array of char*. The size of this array is argc. You should pass an element of this array to strlen.
argv是一个数组char*。这个数组的大小是argc。您应该将此数组的一个元素传递给strlen.
回答by I Have Fish
Perhaps you meant to do something like this:
也许你打算做这样的事情:
size_t argv_length(char** argv)
{
size_t ret = 0;
while( *(++argv) )
ret += strlen(*argv);
return ret;
}
?
?
回答by Ima Miri
argv takes an arryas of char* but you need to pass argc to strlen rather than whole the array. Then you wont get any error on strcat.
argv 采用 char* 的 arryas,但您需要将 argc 传递给 strlen 而不是整个数组。那么你就不会在 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(argc), 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
}
回答by george
//Task = get length of argv
string text=*argv; //assigning charValue to string variable "text"
int l=text.length(); //getting the length of the string & assigning to variale "l"
//this loop just outputs result on the screen
for (int i=0; i<l; i++) {
cout << *(*argv+i) << flush;
}

