I was confused by a line of code I found in a tutorial on C. Here is the code:
int main(int argc, char *argv[]){
...
char **inputs = argv+1; // This is the confusing line
...
return 0;
}
I can't understand how can you assign an array to a pointer like that. I would be glad if someone could clarify this for me. Thanks ahead!
Say you execute a program like this
C:\\Temp>myprog.exe hello world
the operating system takes these strings and puts them together, in an array of null-terminating strings:
{ "myprog.exe", "hello", "world", NULL }
Then it calls main()
and passes it the number of strings (3) as argc
and a pointer to the first string in this array. this pointer is calles argv
, and is of type char**
( char* argv[]
is just a syntactic convenience, semantically equivalent inside function signatures)
but you want inputs
to hold only the string "hello" and "world", so you takes this pointer, argc
, and point to the next element - add one to it:
char **inputs = argv+1;
now inputs
points toward { "hello", "world", NULL }
.
argv
is an array pointers to strings, last pointer is null. Suppose your executable name is exe
and you run it like:
$ exe fist second
then argv
is:
+----------+
argv---►| "exe" |
+----------+
argv + 1---►| "first" |
+----------+
argv + 2---►| "second" |
+----------+
argv + 3---►| null |
+----------+
* Notice last is null.
So char** input = argv + 1
points to "first" string that is first input argument.
if your prints argv[0]
with %s
output will be "exe"
that is your executable name and if you prints input[0]
with %s
output will be "fisrt"
string.
Note: even if you don't pass any argument intput
will point to NULL (valid address).
(purpose of this is to point to input arguments strings, or say skip program name "exe"
)
My following code example, and its set of outputs will help you to understand.
code.c:
#include<stdio.h>
int main(int argc, char* argv[]){
char** input = argv + 1;
while(*input) /* run untill input-->null */
printf("%s \n", *input++);
return 1;
}
outputs:
~$ gcc code.c -Wall -pedantic -o exe
~$ ./exe
~$ ./exe first
first
~$ ./exe first second
first
second
Array Name is a constant pointer to the first element of array
MORE:- http://forum.allaboutcircuits.com/showthread.php?t=56256
Arrays used in function arguments are always converted to pointers.
So char *argv[]
is the same as char **argv
.
argv[0]
contains the program name (or the name used to invoke the program, in the case of a multiply-linked file), so argv+1
is just the program's arguments.
argv
is not an array in this context; it is a pointer value. In the context of a function parameter declaration, T a[N]
and T a[]
are equivalent to T *a
; in all three declare a
as a pointer to T
.
However, it would still be possible to make the assignment even if argv
were an array. Unless it is the operand of the sizeof
or unary &
operators, an expression of type "N-element array of T
" is converted ("decays") to an expression of type "pointer to T
", and the value of the expression is the address of the first element of the array.
Notice that
char *argv[]
Is an array of pointers. An array declaration, is a pointer itself, argv is a pointer. Since here we have an array of pointers, argv is a pointer to a pointer, just like char **inputs, thus
char **inputs = argv+1;
Is just saying inputs is equal to the pointer argv plus one and since argv+1 is also a pointer, then you have a pointer to a pointer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.