📜  *argv[] **argv (1)

📅  最后修改于: 2023-12-03 14:59:00.419000             🧑  作者: Mango

*argv[] and **argv in C

In C programming language, the pointer symbol (*) is used to access memory locations directly. The *argv[] and **argv are commonly used in C programming to pass arguments to a function. In this article, we will discuss *argv[] and **argv.

*argv[]

*argv[] is a pointer to an array of strings. It is a parameter that is passed to the main() function when the C program is executed. The parameter *argv[] stands for the argument vector. This pointer is used to access the command line arguments in a C program.

For example:

int main(int argc, char *argv[]) {
    // argc is the number of arguments passed
    // argv is an array of pointers to the arguments
    // code here
    return 0;
}

In this example, argc is an integer that represents the number of arguments passed, and argv is a pointer that points to an array of string pointers that represent the arguments passed.

**argv

**argv is a pointer to a pointer to a string. It is used to pass a two-dimensional array of strings to a function. This pointer is commonly used to pass arguments to a function that expects a two-dimensional array.

For example:

void func(char **argv) {
    // code here
}

int main() {
    char *args[] = {"arg1", "arg2", "arg3"};

    func(args);

    return 0;
}

In this example, we define a function that expects a two-dimensional array of strings as an argument. We declare an array of pointers to strings called args, and then pass this array to the function using the **argv pointer.

Conclusion

In summary, *argv[] and **argv are commonly used in C programming to pass arguments to a function. *argv[] is a pointer to an array of strings used to access command line arguments, while **argv is a pointer to a pointer to a string used to pass a two-dimensional array of strings to a function. By understanding these pointers, programmers can better manipulate arguments in C programs.