Is the identifier of a C array converted to a pointer to the first element of the array or to the address of the first element of the array?

huangapple go评论57阅读模式
英文:

Is the identifier of a C array converted to a pointer to the first element of the array or to the address of the first element of the array?

问题

  1. 所以说"一个数组标识符被转换为指向数组第一个元素的指针,或者说一个数组标识符被转换为第一个元素的地址"是正确的吗?

这让我有点困惑...

考虑一下这个小代码:

int a = 10;
&a;
  1. 在一般意义上,&a 是一个“指针”吗(即使指针在技术上是一个保存另一个变量地址的变量)?所以,对象的地址是否在一般意义上被视为指向该位置的“指针”,即使它没有分配给指针变量?

  2. 如果我执行类似于 ((&a) + 1) 的操作,我在技术上正在进行指针运算,这有点意味着 &a 在某种程度上是一个指针,不是吗?

英文:

A C book says "When an array identifier appears in an expression, the type of the identifier is converted from "array of T' to "pointer to T," and the value of the identifier is converted to a pointer to the first element of the array".

However the C programming language ( by Kernighan and Ritchie ) says "By definition, the value of a variable or expression of type array is the address of element zero of the array".

  1. So is it correct to say "that an array identifier is converted to a pointer that points to the first element of the array or that an array identifier is converted to the address of the first element"?

This got me a bit confused...

Consider this small code :

int a = 10;
&a;

2)Is &a a "pointer" in the general sense ( even if a pointer is technically a variable that holds the address of another variable ) ? So is an address of an object considered a "pointer" to that location in the general sense even if it is not given to a pointer variable ?

3)If I do something like ((&a) + 1) I'm technically doing pointer arithmetics, so it kind of implies that &a is a pointer in a certain way, doesn't it ?

答案1

得分: 2

"Pointer"和"address"有时可以互换使用。指向X的指针和X的地址在很大程度上是相同的东西。

当我们试图进行精细区分时,我们可以将"pointer"区分为C语言中的一个对象,其值提供对一个对象或函数的引用,而"address"则是一个值,告诉我们在内存中对象的位置。(一个地址也可以告诉我们一个函数在哪里,或者至少它的入口点在哪里,但这更加复杂。)"Pointer"也可以用来指代与特定指针类型相关联的地址。

在这个更细致的意义上,地址是指针的值,就像整数是int的值一样。尽管如此,我们通常在语言上不那么精确,而是将"the int 3"称为"the int value 3"。

在大多数情况下,你可以将"数组的第一个元素的指针"和"数组的第一个元素的地址"视为相同的东西。

英文:

“Pointer” and “address” are used somewhat interchangeably. A pointer to X and the address of X are largely the same thing.

When we seek to make fine distinctions, we might distinguish “pointer” as an object in the C language whose value provides a reference to an object or a function and “address” as a value that tells where in memory an object is. (An address may also tell where a function is, or at least where its entry point is, but this is more complicated.) “Pointer” might also be used to refer to an address in association with a specific pointer type.

In this finer sense, an address is the value of a pointer, the same way an integer is the value of an int. Nonetheless, we are often less precise with language and refer to the “the int 3” rather than “the int value 3”.

For the most part, you can treat “a pointer to the first element of the array” and “the address of the first element of the array” as the same thing.

huangapple
  • 本文由 发表于 2023年7月28日 02:07:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定