Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

argv pointer to an array of pointers

I am confused as to how the following passage matches up with the code that follows it:

Since argv is a pointer to an array of pointers, we can manipulate the pointer rather than index the array. This next variant is based on incrementing argv, which is a pointer to pointer to char, while argc is counted down:

#include <stdio.h>
/* echo command-line arguments; 2nd version */
main(int argc, char *argv[])
{
    while (--argc > 0)
        printf("%s%s", *++argv, (argc > 1) ? " " : "");
    printf("\n");
    return 0;
}

Isn't char *argv[] just an array of pointers? Wouldn't a pointer to an array of pointers be written as char *(*argv[]) or something similar?

As a side note, is it normal that in general I find declarations that mix arrays and pointers rather confusing?

like image 396
ericgrosse Avatar asked Jun 22 '13 20:06

ericgrosse


People also ask

Is argv a pointer or array?

The second parameter, argv (argument vector), is an array of pointers to arrays of character objects. The array objects are null-terminated strings, representing the arguments that were entered on the command line when the program was started.

Is argv a pointer?

The first element of the array, argv[0] , is a pointer to the character array that contains the program name or invocation name of the program that is being run from the command line. argv[1] indicates the first argument passed to the program, argv[2] the second argument, and so on.

What is argv array?

The argv parameter is an array of pointers to string that contains the parameters entered when the program was invoked at the UNIX command line. The argc integer contains a count of the number of parameters. This particular piece of code types out the command line parameters.

What type is argv [] in C?

argv is an array of pointers to characters.


1 Answers

Such terms as "pointer to array" or "to point to an array" are often treated rather loosely in C terminology. They can mean at least two different things.

In the most strict and pedantic sense of the term, a "pointer to array" has to be declared with "pointer to array" type, as in

int a[10];
int (*p)[10] = &a;

In the above example p is declared as a pointer to array of 10 ints and it is actually initialized to point to such an array.

However, the term is also often used is its less formal meaning. In this example

int a[10];
int *p = &a;

p is declared as a mere pointer to int. It is initialized to point to the first element of array a. You can often hear and see people say that p in this case also "points to an array" of ints, even though this situation is semantically different from previous one. "Points to an array" in this case means "provides access to elements of an array through pointer arithmetic", as in p[5] or *(p + 3).

This is exactly what is meant by the phrase "...argv is a pointer to an array of pointers..." you quoted. argv's declaration in parameter list of main is equivalent to char **argv, meaning that argv is actually a pointer to a char * pointer. But since it physically points to the first element of some array of char * pointers (maintained by the calling code), it is correct to say semi-informally that argv points to an array of pointers.

That's exactly what is meant by the text you quoted.

like image 111
AnT Avatar answered Sep 28 '22 08:09

AnT