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

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

问题描述

几乎所有阅读本文的人都可能熟悉关于 C 的以下三个关键事实:

  1. 当您在表达式中提及数组的名称时,它的计算结果(大多数情况下)是指向数组第一个元素的指针.
  2. 数组下标"运算符 [] 对指针和对数组的作用一样好.
  3. 一个看似数组的函数参数实际上声明了一个指针.

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

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

我以为我是老式的,但我一直称它为C 中数组和指针之间的等价",或简称为数组/指针等价".但我了解到你几乎不能在 SO 上说这些话;他们几乎是禁忌.

这可能看起来像是一个抽象或哲学问题,所以为了更具体地构建它,我正在寻找的是一个简单的名词或名词短语,我可以在句子是的,由于_____,数组下标可以被认为是指针算术的语法糖",例如,这个问题.

(但请注意,我不是寻找那个问题的答案,或者寻找'等效'这个词有什么问题?"这个问题的答案.是的,我知道,它可以误导学习者想象数组和指针在某种程度上是相同的.我在写 时确实有这种困惑此常见问题列表条目.)

解决方案

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

不,事实上它只适用于指针.每当您在表达式中键入 [] 时,您总会得到一个指向第一个元素的指针.这肯定会发生,因为 arr[i] 必须等价于 *(arr + i).前者是后者的语法糖".

<块引用>

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

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

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

<小时>

(我不喜欢等价"这个词,因为数组可以变成指针,但反过来不行.确实,总是有无数初学者提出诸如数组和指针是一回事"之类的困惑信念.称它们为等效"并没有完全帮助.)

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

  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.

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?

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. The "array subscripting" operator [] works just as well for pointers as it does for arrays.

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. A function parameter that seems to be an array actually declares a pointer.

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.

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天全站免登陆