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?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
user1787351
  • 223
  • 1
  • 2
  • 5

8 Answers8

45
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.

LihO
  • 41,190
  • 11
  • 99
  • 167
  • 1
    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? – user1787351 Sep 06 '13 at 04:13
  • 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. – VeraKozya Feb 02 '18 at 22:46
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

JackCColeman
  • 3,777
  • 1
  • 15
  • 21
3
int count = 0; 
while(argv[++count] != NULL);

Now, count will have the length of argv

Kidambi Manoj
  • 131
  • 1
  • 2
  • 10
  • 2
    `!= NULL` is not needed. You could just do something like `while(argv[++count]);` – Brian Aug 27 '18 at 19:23
  • 1
    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. – urnonav Jan 27 '19 at 15:36
2

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

user123
  • 8,970
  • 2
  • 31
  • 52
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;
}

?

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.

bluish
  • 26,356
  • 27
  • 122
  • 180
user1787351
  • 223
  • 1
  • 2
  • 5
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++]);

}
Julien
  • 2,747
  • 1
  • 12
  • 17
RandyMcMillan
  • 59
  • 2
  • 3
-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
}
Ima Vafaei
  • 927
  • 1
  • 14
  • 32