指针变量只是带有某些运算符的整数还是“符号"?

原始单词选择令人困惑.象征"一词比原来的(神秘")好得多.

The original word choice was confusing. The term "symbolic" is much better than the original ("mystical").

在关于我之前的 C++ 问题的讨论中,我被告知指针是

In the discussion about my previous C++ question, I have been told that pointers are

  • "一个很像的简单值类型一个整数"
  • 不是神秘的"
  • "位模式(对象表示) 包含普通可复制类型的值(值表示)(第 3.9/4 节),这是一个指针."

这听起来不对!如果没有什么是象征性的并且指针是它的表示,那么我可以执行以下操作.我可以吗?

This does not sound right! If nothing is symbolic and a pointer is its representation, then I can do the following. Can I?

#include <stdio.h>
#include <string.h>

int main() {
    int a[1] = { 0 }, *pa1 = &a[0] + 1, b = 1, *pb = &b;
    if (memcmp (&pa1, &pb, sizeof pa1) == 0) {
        printf ("pa1 == pb
");
        *pa1 = 2;
    }
    else {
        printf ("pa1 != pb
");
        pa1 = &a[0]; // ensure well defined behaviour in printf
    }
    printf ("b = %d *pa1 = %d
", b, *pa1);
    return 0;
 }

这是一个 C 和 C++ 问题.

This is a C and C++ question.

使用 在线编译和执行 C 进行测试,使用 GNU GCC v4.8.3:gcc -O2 -Wall 给出

Testing with Compile and Execute C Online with GNU GCC v4.8.3: gcc -O2 -Wall gives

pa1 == pb                                                                                                                                                                                       
b = 1 *pa1 = 2    

使用 在线编译和执行 C++ 进行测试,使用 GNU GCC v4.8.3:g++ -O2 -Wall

Testing with Compile and Execute C++ Online with GNU GCC v4.8.3: g++ -O2 -Wall

pa1 == pb                                                                                                                                                                                       
b = 1 *pa1 = 2        

所以通过(&a)[1]修改b失败,在C和C++中使用GCC.

So the modification of b via (&a)[1] fails with GCC in C and C++.

当然,我想要一个基于标准引述的答案.

Of course, I would like an answer based on standard quotes.

为了回应对 &a + 1 上 UB 的批评,现在 a 是一个包含 1 个元素的数组.

To respond to criticism about UB on &a + 1, now a is an array of 1 element.

相关:解引用包含对象地址的越界指针(数组数组)

附加说明:我认为,神秘"一词最初是由 Tony Delroy 此处.我借错了.

Additional note: the term "mystical" was first used, I think, by Tony Delroy here. I was wrong to borrow it.

推荐答案

C 被认为是一种指针和整数密切相关的语言,确切的关系取决于目标平台.指针和整数之间的关系使该语言非常适合用于低级或系统编程的目的.出于以下讨论的目的,我将这种语言称为低级 C"[LLC].

C was conceived as a language in which pointers and integers were very intimately related, with the exact relationship depending upon the target platform. The relationship between pointers and integers made the language very suitable for purposes of low-level or systems programming. For purposes of discussion below, I'll thus call this language "Low-Level C" [LLC].

C 标准委员会编写了对不同语言的描述,其中没有明确禁止这种关系,但不会以任何有用的方式承认,即使实现为目标生成代码和应用领域,这种关系将是有用的.我将这种语言称为仅高级 C"[HLOC].

The C Standards Committee wrote up a description of a different language, where such a relationship is not expressly forbidden, but is not acknowledged in any useful fashion, even when an implementation generates code for a target and application field where such a relationship would be useful. I'll call this language "High Level Only C" [HLOC].

在编写标准的年代,大多数自称为 C 实现的东西处理的是 LLC 的方言.大多数有用的编译器处理一种方言,该方言在比 HLOC 更多的情况下定义了有用的语义,但没有 LLC 那么多.指针的行为是更像整数还是更像抽象的神秘实体取决于人们使用的是哪种方言.如果正在进行系统编程,将 C 视为密切相关的指针和整数是合理的,因为适合该目的的 LLC 方言这样做,而不这样做的 HLOC 方言不适合该目的.然而,在进行高端数字运算时,人们往往会使用无法识别这种关系的 HLOC 方言.

In the days when the Standard was written, most things that called themselves C implementations processed a dialect of LLC. Most useful compilers process a dialect which defines useful semantics in more cases than HLOC, but not as many as LLC. Whether pointers behave more like integers or more like abstract mystical entities depends upon which exact dialect one is using. If one is doing systems programming, it is reasonable to view C as treating pointers and integers as intimately related, because LLC dialects suitable for that purpose do so, and HLOC dialects that don't do so aren't suitable for that purpose. When doing high-end number crunching, however, one would far more often being using dialects of HLOC which do not recognize such a relationship.

真正的问题和这么多争论的根源在于,LLC 和 HLOC 的分歧越来越大,但它们都被称为 C.

The real problem, and source of so much contention, lies in the fact that LLC and HLOC are increasingly divergent, and yet are both referred to by the name C.

相关文章