argv指向指针数组的指针 [英] argv pointer to an array of pointers

查看:113
本文介绍了argv指向指针数组的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下段落与后面的代码如何匹配感到困惑:

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

由于argv是指向指针数组的指针,因此我们可以对 指针而不是索引数组.下一个变种基于 argv递增,这是指向char的指针,而argc 倒数:

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;
}

不是char *argv[]只是一个指针数组吗?不会将指向指针数组的指针写为char *(*argv[])或类似的符号吗?

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?

推荐答案

在C术语中,诸如指向数组的指针"或指向数组的指针"之类的术语通常比较宽松.它们可能意味着至少两种不同的事物.

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;

在上面的示例中,p被声明为指向10个int s数组的指针,并且实际上已初始化为指向这样的数组.

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被声明为仅指向int的指针.初始化为指向数组a的第一个元素.您经常会听到并看到人们说p在这种情况下也指向int的数组",即使这种情况在语义上与先前的情况有所不同.在这种情况下,指向数组的点"表示通过指针算术提供对数组元素的访问",如p[5]*(p + 3)一样.

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).

这正是您引用的短语"... argv是指向指针数组的指针..."的含义.在main的参数列表中argv的声明等同于char **argv,这意味着argv实际上是指向char *指针的指针.但是,由于它物理上指向char *指针数组(由调用代码维护)的第一个元素,因此半正式地说argv指向指针数组是正确的.

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.

这篇关于argv指向指针数组的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆