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

查看:28
本文介绍了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 数组的指针,它实际上被初始化为指向这样一个数组.

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 是指向指针数组的指针..."的意思.argvmain的参数列表中的声明等价于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天全站免登陆