C编译器如何知道char ** x指向数组? [英] How does a C Compiler know that char** x points to an array?

查看:85
本文介绍了C编译器如何知道char ** x指向数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C语言中,我读到char** xchar* x[]的含义完全相同.我了解第二个示例,但我不了解第一个示例.

对我来说,第一个示例是说指向字符的指针的指针",但是C会将其视为指向字符数组的指针的列表"?

由于我一直很难掌握它,有人可以用外行的方式向我解释这一点.

我承认我的第一句话是我想让人想听到的内容,而不是我的实际解释.我也将其视为指向char的指针的指针,但是当我阅读该问题的最高答案时,我的理解下降了:解决方案

长话短说-不能区分.编译器不知道它指向一个数组.它所看到的只是一个指针. 您显示的方式 (作为函数参数声明的一部分-传递给函数)char *[]char **表示同一件事.这是什么?指向char*的指针.没什么.

编译器没有保留任何额外的信息,这可以区分是传递char*数组还是传递char**数组,因为最终char*数组最终会衰减为指向第一个元素的指针-char**. >

尝试一下,您将了解:-

char c = 'a';
char *pc = &c;
char **ppc = &pc;
f(ppc);

....

void f(char *ppc[]){

}

这不会使编译器抱怨.因为编译器最终将其视为char **ppc.

要证明编译器在将数组传递给函数时看不到数组-您可以看到此引号(它基本上表示存在衰减,并且归结为指向第一个元素的指针.因此,它看到的是全部)(通过的方式得出的结论是,char**char*[] all 情况下由于衰减而相同-并非如此)

§ 6.3.2.1 ¶3标准N1570

除非它是sizeof运算符,_Alignof运算符或一元&运算符的操作数,或者是用于初始化数组的字符串文字,类型为' 类型数组"将转换为类型指针"的表达式,该表达式指向数组对象的初始元素,而不是左值.如果数组对象具有寄存器存储类,则该行为是不确定的.

进行此编辑是为了清除导致某人不满意答案的误解.

In C, I've read that char** x and char* x[] mean exactly the same thing. I understand the 2nd example, but I do not understand the 1st example.

To me the first example is saying "pointer to a pointer to a character", but C will see this as "point to a list of pointers to character arrays"?

Would someone be able to explain this to me in layman's terms as I've been having an unusually difficult time grasping it.

EDIT: I admit I worded the first bit as what I thought people would want to hear and not my actual interpretation. I too see it as a pointer to a pointer to a char but my understanding fell when I read the top answer on this question: Difference between char *argv[] and char **argv for the second argument to main()

Which shows that it is being used like a pointer to an array of character pointers when used as an argument vector...

解决方案

Long story short - it doesn't distinguish that. Compiler doesn't know that it points to an array. All it sees is a pointer. The way you have shown(as part of function parameter declaration - passing to the function) char *[] and char ** denotes the same thing. What it is? Pointer to a char*. Nothing more than that.

There is no extra information kept by a compiler that makes it distinguish whether you passed a char* array or a char** because ultimately char* array decays into pointer to first element - which is char**.

Try this, you would understand:-

char c = 'a';
char *pc = &c;
char **ppc = &pc;
f(ppc);

....

void f(char *ppc[]){

}

This won't make compiler complain. Because it is ultimately considered by compiler as char **ppc.

To prove that arrays are not seen by the compiler when it is passed to the function - you can see this quote (which basically says that there is decay and it boils down to pointer to first element. So it sees a pointer that's all) (This by no way give the idea that char** and char*[] are same in all cases because of decaying - it is not)

§6.3.2.1¶3 standard N1570

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type 'array of type' is converted to an expression with type 'pointer to type' that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

The edit is been made to clear the misinterpretation that got someone to not appreciate the answer.

这篇关于C编译器如何知道char ** x指向数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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