argv
is a array of character pointers, that means argv
is going to store the address of all the strings which you passed as command line argument.
so argv[0]
will gives you the address of first string which you passed as command line argument, that you are storing into the pointer variable array
in main
function.
Now you have to pass only the address to the function foo
but you are passing the first character of that string. For example if your first command line argument is temp.txt
you are passing character t
to the function foo
. So inside foo
function you are having a char pointer variable array
, in that ASCII value of the character t
will be assigned. And then you are passing that to printf
, which will treads that ASCII value as address, and it will tries to access that address to print which will leads to crash (unexpected behaviour).
So you have to pass only the address of the command line argument to the function foo
like below.
foo(array);
printf(array)
- Here printf
will treads the format specifier as string(%s
) and it will tries to print all the characters starting from the address array
untill it meets a null character \0
.
But better to add the printf
like below
printf("%s", array);