#include
#include
#include
int main(int argc, char *argv[]){
int fir; //badly named loop variable
char *in
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
.
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;
}
?
int count = 0;
while(argv[++count] != NULL);
Now, count will have the length of argv
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
}
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.
argv
is an array of char*
. The size of this array is argc
. You should pass an element of this array to strlen
.