19
#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
  • 330,190
  • 66
  • 504
  • 555
user1787351
  • 203
  • 1
  • 2
  • 5

9 Answers9

41
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
  • 39,598
  • 10
  • 94
  • 164
  • 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
11

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,707
  • 1
  • 13
  • 20
3
int count = 0; 
while(argv[++count] != NULL);

Now, count will have the length of argv

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

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,760
  • 2
  • 29
  • 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
  • 24,718
  • 26
  • 114
  • 174
user1787351
  • 203
  • 1
  • 2
  • 5
0

If you want size of "outer" array of argv - argc is the size.

If you want size of "inner" array of argv:

int len(std::string word){
  return word.size();
}

len(argv[1]);
-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 Miri
  • 777
  • 12
  • 31
-1
//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;
    }
george
  • 1
  • 2
  • 1
    Your answer would be really improved if you could add some description about what your suggestion does. – EcologyTom Aug 16 '18 at 10:28
  • sorry. original task was to copy/append one array into another. to do that you need array length. sizeof(argv) / sizeof(char) will not work. so for me the easiest way to get the length was to simply 1) convert argv to string 2) identify length of that string by using "length()". @EcologyTom – george Aug 16 '18 at 11:15
  • Good stuff, considered editing your answer so that all the information is in the answer and future readers won't have to read all the comments to find all the details. Welcome to SO! – EcologyTom Aug 16 '18 at 11:17
  • The question is tagged C, not C++. – aschepler Aug 16 '18 at 11:49