指针可以指向C中的自身内存地址吗? [英] Can pointer point to itself memory address in C?

查看:102
本文介绍了指针可以指向C中的自身内存地址吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,指针指向其自己的内存地址.

In the following code, a pointer points to its own memory address.

#include <stdio.h>

int main() 
{
    void * ptr;
    ptr = &ptr;

    return 0;
}

如果指针能够指向其自己的内存地址,这是否有意义?

推荐答案

不,这没有意义. 如果可以找到变量ptr,则可以执行& ptr. 它将为您提供与ptr内容相同的结果.

No, it doesn't make sense. If you can find variable ptr, you can just do &ptr. It will give you the same results as the contents of ptr.

此外,由于ptr只能告诉自己一些信息,因此无论如何都没有用.它没有提供任何对程序其余部分有意义的信息.

Moreover since ptr only tells something about itself, it's useless anyhow. It doesn't provide any info meaningful to the rest of your program.

请考虑一下,有一个例外.您可以使用ptr ==& ptr作为一种特殊值的情况,例如NULL.因此,在那种情况下,我认为它很有用.

有趣的是,在那种情况下,值& ptr恰好是一个特殊值,而它像NULL这样就没有意义作为某物的地址.

The fun of it is that in that case the value &ptr makes sense as a special value precisely while it doesn't make sense as the address of something, just like NULL.

这篇关于指针可以指向C中的自身内存地址吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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