Designation ** argv in the main function

Possible duplicate:
argc and argv in the main

I find it difficult to understand the notation used to declare a common core function, i.e. int main(int argc, char *argv[]) . I understand that what is actually passed to the main function is a pointer to a pointer to a char , but I find the notation difficult. For instance:

Why is **argv pointing to the first char and not the whole line? Similarly, why *argv[0] points to the same as in the previous example.

Why does *argv point to the entire first line instead of the first char , as in the previous example?

This is a bit unrelated, but why does *argv + 1 indicate the string 'minus the first char' instead of pointing to the next line in the array?

+4
source share
5 answers

Consider a program with argc == 3 .

  argv | v +---------+ +----------------+ | argv[0] |-------->| program name\0 | +---------+ +-------------+--+ | argv[1] |-------->| argument1\0 | +---------+ +-------------+ | argv[2] |-------->| argument2\0 | +---------+ +-------------+ | 0 | +---------+ 

The argv variable points to the beginning of an array of pointers. argv[0] is the first pointer. It indicates the name of the program (or, if the system cannot determine the name of the program, the string for argv[0] will be an empty string; argv[0][0] == '\0' ). argv[1] points to the first argument, argv[2] points to the second argument and argv[3] == 0 (equivalent to argv[argc] == 0 ).

Other details you need to know of course is that array[i] == *(array + i) for any array.

You specifically ask:

  • Why is ** argv pointing to the first char, and not to the whole line?

*argv equivalent to *(argv + 0) and therefore argv[0] . This is char * . When you cast char * , you get the "first" character in the string. And **argv therefore equivalent to *(argv[0]) or *(argv[0] + 0) or argv[0][0] .

(We can justifiably say that **argv is a symbol, not a pointer, so it does not point to the first char. This is just another name for 'p' "program name\0" .)

  • Similarly, why * argv [0] points to the same as in the previous example.

As noted above, argv[0] is a pointer to a string; therefore *argv[0] must be the first character in the string.

  • Why does *argv point to the entire first line instead of the first char, as in the previous example?

This is a matter of convention. *argv points to the first character of the first line. If you interpret it as a pointer to a string, it points to an "entire string", just like char *pqr = "Hello world\n"; points to the "whole line". If you interpret it as a pointer to a single character, it points to the first character of the string. Think of it as the duality of a wave particle, only here it is a duality of character.

  • Why does *argv + 1 indicate the string 'minus the first char' instead of pointing to the next line in the array?

*argv + 1 (*argv) + 1 . As already discussed, *argv points to the first character of the first line. If you add 1 to the pointer, it points to the next element; since *argv points to a character, *argv+1 points to the next character.

*(argv + 1) indicates the (first character) of the next line.

+10
source

It all comes down to pointer arithmetic.

 *argv[0] = *(*(argv + 0)) = **argv 

Since [] has a higher priority than unary * .

On the other hand, *argv gives the first cell in the array, an array containing pointers. What does this pointer indicate? Why is the array char, string, of course.

*argv + 1 gives what it gives, because + has a lower priority than unary * , so first we get a pointer to a string, and then add 1 to it, thus obtaining a pointer, the second character in the string.

+3
source

I understand that what is actually passed to the main function is a pointer to a pointer to char

No, what has passed is an array of char pointers (an array of characters). Think of it this way if I give it on the command line:

 >> ./program hello 456 

My main program will receive:

 argc == 3 argv[0] == program (the name of the program as a string) argv[1] == hello (the first parameter as a string) argv[2] == 456 (the second parameter as a string) 

Why does **argv point to the first char and not the whole string?

 char *argv[] //an array of character pointers *argv // an array decays to a pointer, so this is functionally equivalent to // argv[0] **argv // Now the argv[0] decays to a pointer and this is functionally // equivalent to (argv[0])[0] 

Likewise, why does *argv[0] point to the same thing as the previous example.

See above.

Why does *argv point to the whole first string, instead of the first char like the previous example?

See above.

+2
source

char ** argv , contains input that you can provide to your application c. Each argv position will contain a string, a string in c is represented by an array of characters.

So, since you can give more than one argument to your application, this means that you can have more than one row, therefore more than one char array. So char ** argv .

When you do. / your _application 1 10 3. argv has 1 10 3:

 argv[0] = "1"; argv[1] = "10"; argv[2] = "3"; 

Argc will tell you the number of arguments received by your_cell. In this example, argc = 3.

When you know the size, you can do something like this char array [M] , when you don’t know manually the size you make char * array;. Basically saying that a variable array will point to the first memory position of a future array of characters.

You can go deeper, you need to have several lines, so you can make a char array [N] [M] , or if you don't know the size of the char ** array . On the same line, the first pointer points to the first position of the memory of the character array and the second pointer to char of the first character array.

Why is ** argv pointing to the first char, and not to the whole line?

Because argv is an array of character arrays, so the first point already points to the first character array, so the second pointer points to the first char of this array. So * argv [0] , the same one, because the pointer points to an array of characters, and you specify the position you need, in this case it is zero, so this will be the first char from the array that you are pointing to.

You can also specify the char array that you want to do something like argv [1] [0] , in which case you are pointing to the first char of the second array,

The 2D array is sequentially allocated in memory, so ** argv points to the first position of the 2D array, because you need to know where the 2D arrays will run.

After 2D is selected in memory, you can know that you can directly specify the position you want to access, for example, 2D_array [1] [3] ;

0
source

This is all because the array is also a pointer to the first element of the array in c. **argv double changes our pointer to a pointer to char, giving us char. *argv[0] basically says "play this address and return the first element in the array described by the address we just got from dereferencing", which turns out to be the same thing. *argv only dereferencing times, so we still have a pointer to a char or a char array. *argv + 1 dereference times, giving us the first line of characters, and then adds 1 to the address, indicating to us the address of the second element. Since pointers are also arrays, we can say that this is an array *argv minus the first element.

-1
source

Source: https://habr.com/ru/post/1445794/


All Articles