一个指针都不能指向自己? [英] Can a pointer ever point to itself?

查看:213
本文介绍了一个指针都不能指向自己?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人提到这个问题<一个href=\"http://stackoverflow.com/questions/50447/favorite-c-interview-question/1477130#1477130\">here.

我的问题是:如果一个指针变量具有相同的地址作为它的值,是不是真的指向自身

例如 - ?在下面的一段code,也是 A 指向自己的指针

For example - in the following piece of code, is a a pointer to itself?

#include<stdio.h>

int main(){
   int* a;
   int b = (int)&a;
   a = b;
   printf("address of a = %d\n", &a);
   printf("  value of a = %d\n", a);
}

如果 A 不是一个指向自身,则同样的问题再次提出:?可以将指针指向其自身的结果
此外,如何是一种自我指向指针有用吗?

If a is not a pointer to itself, then the same question poses again: Can a pointer point to itself?
Also, how is a self pointing pointer useful?

推荐答案

你实际上做的有不可以具有指针指向自身。您的使用分配给指针的内存空间来存储指针的位置。一个指向一个int点整数 - 从来没有给其他指针整数,包括其自身

What you're actually doing there is not having the pointer point to itself. You are using the memory space allocated for the pointer to store the location of the pointer. A pointer to an int points to ints - never to other pointers to ints, including itself.

例如,假设您创建一个指向 A

For example, let's say you create a pointer a:

int * a;

这都有自己的现货存储:

It gets its own spot in memory:

   4     a (5)    6
[....][00000000][....]

在这个简单的例子,比方说,一个是内存位置'5'。

In this simple example, let's say a is at memory location '5'.

如果你做到这一点:

a = (int*)&a;

...下面会发生:

...the following would happen:

   4     a (5)    6
[....][00000005][....]

这里发生的事情是, A 指向它的想法是在位置5的整数。这也恰好是&放大器;一个指向,但在上下文是什么 A 是指向,它现在指向在位置5的整数 - 和该整数是5。

What's happening here is that a is pointing to what it thinks is an integer at location 5. This also happens to be the same memory location that &a is pointing to, but in the context of what a is pointing to, it's now pointing to the integer at location 5 - and that integer is 5.

例如这两个会的工作:

cout<<(int)a;//outputs 5
cout<<*a;//Outputs the integer at memory location 5 - which is 5.

如果你想创建一个指针,你最肯定可以 - 通过以下两种方式:

If you wanted to create a pointer to a, you most definitely could - either of these ways:

int **b = (int**)a;

int ** b = &a;

搜索结果

但要认识到, A 不是指针本身是非常重要的。这是在该位置的指针的整数它存储的 - 这恰好是相同的它自己的位置。
结果

But it's very important to realize that a isn't a pointer to itself. It's a pointer to the integer at the location it stores - which just happens to be the same as its own location.

要进一步展示(通过一个更简单的例子),这是怎么回事,类似的东西可以用 INT 发生。也就是说,你可以存储的存储位置 INT 中本身:

To further show (through an even simpler example) what's going on, something similar could happen with an int. That is, you can store the memory location of an int within itself:

int a=999;

A 目前在存储器中的位置,并具有999的值(我们将pretend它被放置在存储位置'46'):

a now has a location in memory, and has a value of 999 (we'll pretend it's been placed in the memory location '46'):

  45     a (46)   47
[....][00000999][....]

这是在定位'46' - 如果我们想要的,我们可以存储在这个数字为整数 A

a=(int)&a;

  45     a (46)   47
[....][00000046][....]

现在 A 等于&放大器;一个价值,但的不是类型 - A 只是一个整数,它现在不指向其自身神奇只是因为我们用它来存储自己的内存位置

and now a is equal to &a in value, but not in type - a is just an integer, it doesn't point to itself magically now just because we used it to store its own memory location.

这篇关于一个指针都不能指向自己?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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