“数组/指针等效"的现代术语是什么? [英] What's a modern term for "array/pointer equivalence"?

查看:104
本文介绍了“数组/指针等效"的现代术语是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于每个阅读本书的人来说,都可能熟悉有关C的以下三个关键事实:

Just about everyone reading this is probably familiar with these three key facts about C:

  1. 当您在表达式中提及数组的名称时,它会(大部分时间)对指向数组第一个元素的指针求值.
  2. 数组下标"运算符[]对指针的作用与对数组的作用一样.
  3. 一个似乎是数组的函数参数实际上声明了一个指针.
  1. When you mention the name of an array in an expression, it evaluates (most of the time) to a pointer to the array's first element.
  2. The "array subscripting" operator [] works just as well for pointers as it does for arrays.
  3. A function parameter that seems to be an array actually declares a pointer.

这三个事实对于C语言中的数组和指针处理绝对是至关重要的.它们甚至不是三个独立的事实.它们是一个核心概念的相互联系的方面.如果不对这个概念有相当的了解,就不可能正确地进行相当基本的C编程.

These three facts are absolutely central to array and pointer handling in C. They're not even three separate facts; they're interlinked facets of one central concept. It is not possible to do even fairly basic C programming properly without a decent understanding of this concept.

我今天的问题很简单,这个概念叫什么名字?

My question today is simply, What is the name for this concept?

我以为我是老式的,但是我一直称其为"C语言中数组与指针之间的对等",或简称为数组/指针对等".但是我了解到,您几乎无法在SO上说这些话.他们是非常忌讳的.

I supposed I'm old-fashioned, but I've always called it "The equivalence between arrays and pointers in C", or "array/pointer equivalence" for short. But I've learned that you almost can't say those words on SO; they're pretty much taboo.

这似乎是一个抽象或哲学问题,因此要更具体地构架,我要寻找的是一个简单的名词或名词短语,我可以在句子中使用是的,由于_____,数组下标可以被认为是指针算术的语法糖",例如回答这个问题.

This may seem like an abstract or philosophical question, so to frame it more concretely, what I'm looking for is is a simple noun or noun phrase I could use in the sentence "Yes, due to _____, array subscripting can be thought of as syntactic sugar for pointer arithmetic", in answer to, say, this question.

(但是请注意,我不是在寻找这个问题的答案,还是在寻找等价性"这个词有什么问题?)的答案.是的,我知道,它可以误导学习者以为数组和指针是相同的.当我写此常见问题解答列表条目.)

(But please note that I am not looking for answers to that question, or for answers to the question "What's wrong with the word 'equivalence'?". Yes, I know, it can mislead learners into imagining that arrays and pointers are somehow the same. I did have that confusion in mind when I wrote this FAQ list entry.)

推荐答案

  1. 数组下标"运算符[]对指针的作用与对数组的作用一样.

不,实际上它仅适用于指针.每当在表达式中键入[]时,总会得到一个指向第一个元素的指针.由于arr[i]必须等效于*(arr + i),因此可以保证会发生这种情况.前者是后者的语法糖".

No, in fact it only works for pointers. Whenever you type [] in an expression, you always get a pointer to the first element. This is guaranteed to happen since arr[i] must be equivalent to *(arr + i). The former is "syntactic sugar" for the latter.

  1. 一个似乎是数组的函数参数实际上声明了一个指针.

这实际上是一种特殊情况,称为数组调整",其中编译器将数组类型的函数参数的声明隐式更改为指向第一个元素的指针.理论上肯定是使函数与表达式的数组衰减"兼容,但是C标准将这些术语分开.

This is actually a special case, referred to as "array adjustment", where the compiler implicitly changes the declaration of a function parameter of array type into a pointer to the first element. The rationale is surely to make functions compatible with the "array decay" of expressions, but the C standard keeps the terms separate.

这两种情况,表达式和函数参数通常被非正式地称为数组衰减".尽管有时它仅用于表达式,而不用于函数参数.我认为该术语没有一个统一的用法.我认为数组衰减"是最好的,尽管C标准没有在任何地方使用该术语.

Both cases, expressions and function parameters, are often referred to informally as "array decay". Though sometimes this is only used for expressions and not for function parameters. I don't think there exists a single, consistent use of the term. "Array decay" is the best one I think, although the C standard does not use that term anywhere.

(我不喜欢等价性"一词,因为数组可以变成指针,但不能相反.事实上,总是有无数的初学者提出混淆的信念,例如数组和指针是同一件事".称它们为等效"完全没有帮助.)

(I dislike the term "equivalence", because an array can turn into a pointer, but not the other way around. Indeed there's always countless beginners coming up with confused beliefs such as "arrays and pointers are the same thing". Calling them "equivalent" doesn't exactly help.)

这篇关于“数组/指针等效"的现代术语是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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