请包括一个例子的解释。


当前回答

来自Pointer Basics的代码和解释:

The dereference operation starts at the pointer and follows its arrow over to access its pointee. The goal may be to look at the pointee state or to change the pointee state. The dereference operation on a pointer only works if the pointer has a pointee -- the pointee must be allocated and the pointer must be set to point to it. The most common error in pointer code is forgetting to set up the pointee. The most common runtime crash because of that error in the code is a failed dereference operation. In Java the incorrect dereference will be flagged politely by the runtime system. In compiled languages such as C, C++, and Pascal, the incorrect dereference will sometimes crash, and other times corrupt memory in some subtle, random way. Pointer bugs in compiled languages can be difficult to track down for this reason.

void main() {   
    int*    x;  // Allocate the pointer x
    x = malloc(sizeof(int));    // Allocate an int pointee,
                            // and set x to point to it
    *x = 42;    // Dereference x to store 42 in its pointee   
}

其他回答

来自Pointer Basics的代码和解释:

The dereference operation starts at the pointer and follows its arrow over to access its pointee. The goal may be to look at the pointee state or to change the pointee state. The dereference operation on a pointer only works if the pointer has a pointee -- the pointee must be allocated and the pointer must be set to point to it. The most common error in pointer code is forgetting to set up the pointee. The most common runtime crash because of that error in the code is a failed dereference operation. In Java the incorrect dereference will be flagged politely by the runtime system. In compiled languages such as C, C++, and Pascal, the incorrect dereference will sometimes crash, and other times corrupt memory in some subtle, random way. Pointer bugs in compiled languages can be difficult to track down for this reason.

void main() {   
    int*    x;  // Allocate the pointer x
    x = malloc(sizeof(int));    // Allocate an int pointee,
                            // and set x to point to it
    *x = 42;    // Dereference x to store 42 in its pointee   
}

指针是一个值的“引用”。就像图书馆的电话号码是一本书的参考资料一样。“取消引用”电话号码是在物理上检查和检索这本书。

int a=4 ;
int *pA = &a ;
printf( "The REFERENCE/call number for the variable `a` is %p\n", pA ) ;

// The * causes pA to DEREFERENCE...  `a` via "callnumber" `pA`.
printf( "%d\n", *pA ) ; // prints 4.. 

如果书不在那里,图书管理员就开始大喊大叫,关闭图书馆,然后几个人开始调查一个人去找不在那里的书的原因。

解除对指针的引用意味着获取指针所指向的内存位置中存储的值。操作符*用于执行此操作,称为解引用操作符。

int a = 10;
int* ptr = &a;

printf("%d", *ptr); // With *ptr I'm dereferencing the pointer. 
                    // Which means, I am asking the value pointed at by the pointer.
                    // ptr is pointing to the location in memory of the variable a.
                    // In a's location, we have 10. So, dereferencing gives this value.

// Since we have indirect control over a's location, we can modify its content using the pointer. This is an indirect way to access a.

 *ptr = 20;         // Now a's content is no longer 10, and has been modified to 20.

简而言之,解引用意味着从指针所指向的某个内存位置访问值。

我认为前面所有的答案都是错误的,因为他们 声明取消引用意味着访问实际值。 维基百科给出了正确的定义: https://en.wikipedia.org/wiki/Dereference_operator

它操作一个指针变量,并返回一个与指针地址上的值等价的l值。这被称为指针的“解引用”。

也就是说,我们可以不引用任何指针 访问它所指向的值。例如:

char *p = NULL;
*p;

我们在没有访问NULL指针的情况下解引用了它 价值。或者我们可以这样做:

p1 = &(*p);
sz = sizeof(*p);

同样,取消引用,但从不访问值。这样的代码不会崩溃: 在实际访问数据时发生崩溃 无效的指针。然而,不幸的是,根据 标准,解除无效指针的引用是未定义的 行为(少数例外),即使你不尝试 触摸实际数据。

简而言之,取消对指针的引用意味着应用 对它的解引用操作符。运算符只是返回一个 l值供您将来使用。