views:

823

answers:

7

This question was mentioned here.

My question is: If a pointer variable has the same address as its value, is it really pointing to itself?

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);
}

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?

+17  A: 
void* p = &p;

It's not terribly useful, but structs that point to themselves are useful in circular lists of length 1:

typedef struct A {
  struct A* next;
} A;

A a = { &a };

Per your exact example, I believe you meant:

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

// which can be simplified to:
int* a = (int*)&a;
Roger Pate
+1, a little more detailed than my answer.
Tim Post
Note that what you did with the struct is different from what he was trying to do. A pointer that points to itself is quite different from a structure that has a pointer to itself. +1 for the example though, as that is probably what he should be thinking of with self-referential pointers.
Phil
Roger Pate
+4  A: 

Well, first I'd change the code around:

int **a;
a = (int **)&a;  // otherwise you get a warning, since &a is int ***

I'm not sure why you would do this, but it is allowed.

printf("The address of a is %p\n", &a);
printf("a holds the address %p\n", a);
printf("The value at %p is %p\n", a, *a); // the *a is why we made a an int **

They should print out the same thing.

The address of a is 0x7fffe211d078
a holds the address 0x7fffe211d078
The value at 0x7fffe211d078 is 0x7fffe211d078

Note that this is not a good idea, as that very first cast a = (int **)&a is a hack to force a to hold a value that it shouldn't hold. You declare it an int ** but try to force an int *** into it. Technically the sizes are the same, but in general don't do that because people expect that an int * holds the address of something that can be used as an int, an so on.

Phil
+1  A: 

Yes, it is possible to point to itself.

int* a;
a = &a;

But not have any use, at least explicitly like this.

zaharpopov
nos
+1  A: 

Yes and no, because the pointer's type is almost as important as the pointer's value.

Yes, a pointer can contain the position of a pointer to itself; even a long can contain the position of a pointer to itself. (Ints usually can, but I don't know if that's guaranteed everywhere.)

However, there is no type to represent this relationship. If you have a pointer which points to itself, you actually have a different type when you dereference it. So:

void *p = &p;
// *p is illegal, even though you probably wanted it to equal 'p'
if( *p != p ) {
    printf("Something's wrong");
}

int *i = (int*)&i;
// The following statement is still illegal
if( *i == i ) {
    printf("The universe works!");
}

I would say the answer is 'no', because it won't work unless you're going to abuse the type system. I think it's an indication that you're doing something wrong (though sometimes it's certainly necessary).

CWF
+1  A: 

Dereferencing a pointer results in a value of its value type (e.g. dereference an int* gives you an int, an int*s value type). For a variable to point to a pointer its value type would have to be int*, which is not the case for an int* as previously stated. So for a pointer to point to itself one would have to do some kind of cast in order to get it by the compiler:

int* a;
a = reinterpret_cast<int*>(&a);

To dereference a, then, you would have an int whose value happens to be the address at which a is located (mod truncation of the address to fit the type), but it is still an int and not an int*, which would require another cast.

A pointer to a pointer is often known as a handle, which is a different type (int**) than a pointer (int*). (Note that an int** handle has value type int*.)

fbrereto
+4  A: 

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 int's - never to other pointers to ints, including itself.

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

int * a;

It gets its own spot in memory:

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

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

If you were to do this:

a = (int*)&a;

...the following would happen:

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

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.

For example both of these would work:

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;

or

int ** b = &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.


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 c=999;

c 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][....]

It's in the location '46' - if we wanted, we could store this number as an integer within a:

a=(int)&a;

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

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.

Cam
+4  A: 

can memory address = FFFFFFFF store value FFFFFFFF?

no problem, don't overthink it.

iterationx