How to find the length of argv[] in C
Asked Answered
A

8

21
#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 strcat function. It also says "given a char ** but expected a const char *" for both functions.

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.

Do the strlen() and strcat() functions not take char arrays?

Anastomosis answered 6/9, 2013 at 3:47 Comment(1)
I think you want an index into that argv[], but don't go outside [0..(argc-1] when you do.Cydnus
S
48
int main(int argc, char *argv[])

argv is an array of pointers to char (i.e. array of strings). The length of this array is stored in argc argument.

strlen is meant to be used to retrieve the length of the single string that must be null-terminated else the behavior is undefined.

Sachasachem answered 6/9, 2013 at 3:51 Comment(2)
I think I understand. I cannot use argv[] because it is not actually an array of char, an array of pointers to char's? if I make a copy of argv[x] and use it in strcat like strcat(input, copyargv[x]) will that work?Anastomosis
argv[ ] is an array of strings (i.e. an array of pointers to char pointers, where each char pointer points to the beginning of a string); argc tells you how many strings you have; strlen returns the length of the passed in string, so it can only be called on a char *, i.e. char [ ], i.e. string): strlen(argv[i]), where i is between 0 and argc.Inequitable
W
12

Not sure why no one has suggested changing strlen to refer to a specific entry in the array of pointers to 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

Wasteful answered 6/9, 2013 at 5:51 Comment(0)
C
3
int count = 0; 
while(argv[++count] != NULL);

Now, count will have the length of argv

Chinchy answered 18/4, 2014 at 20:35 Comment(2)
!= NULL is not needed. You could just do something like while(argv[++count]);Numbersnumbfish
Doesn't this only give you argc? You are counting the number of arguments, not the total length of the string run by the user.Nashua
H
2

argv is an array of char*. The size of this array is argc. You should pass an element of this array to strlen.

Hesperidin answered 6/9, 2013 at 3:50 Comment(0)
P
1

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;
}

?

Projectionist answered 6/9, 2013 at 4:4 Comment(0)
A
1

argv is an array of char, strlen only 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.

Anastomosis answered 7/9, 2013 at 21:54 Comment(0)
F
0

you can drop this into your main, easy peasy, keep it simple :

int argvlen = 0;

while(argv[argvlen] != NULL){

   // NOTE: argvlen++ increment postfix.   
   printf("argv[argvlen++] = %d\n", *argv[argvlen++]);

}
Fugacity answered 17/5, 2023 at 15:16 Comment(0)
M
-1

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.

 #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
}
Mccrory answered 6/9, 2013 at 4:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.