self-pointer

can i make a pointer that points to itself??

i tried it but m not able to write its program..could anyone help plz..
Yes:
 
void* p = &p;


But I can't imagine a use for that.
1.what is void*?
2.is what u wrote similar to

void*p;
p=&p;
?
3.and how to print the address and value to which it points?
1. void* is the type-erased pointer type.

2. Yes, it is equivalent.

3. To get to the value, you will have to first cast the void* to a pointer to the type originally used to initialize it, before erasure. The type of &p was void**, so that what you'd cast to:

1
2
3
4
5
6
7
#include <iostream>
int main()
{
    void* p = &p;
    std::cout << "address: " << p << '\n'
              << "  value: " << *static_cast<void**>(p) << '\n';
}
Last edited on
4. can two pointers point to each other?? the condition is that the pointers shold point to a structure called NODE.

plz sme1 reply asap..m stuck on it
Two pointers can point to each other, but pointing "to a structure called Node" is a completely different question. It would help if you described what you're really stuck on.
ok..the problem is..
there is a structure called node

1
2
3
4
5
6
7
8
9
10
11
struct node
{
    int info;
    struct node* next;
}

int main()
{
      node* start;
      node* stop;
}


now, i want these two pointers start and stop to point to each other.
It wouldn't make sense for those two pointers to "point to each other", even if it was possible. What is the task you're trying to solve?
well..i want that initially both these pointers point to each other.

then if i call an insert_node function, then the function will create a node and will insert it between start and stop so that now, start and stop will both point to this node.

then, again if i call insert_node function, then it will again create a node and insert it between start and the node previously created so that now, start points to new node and stop points to old node.

actually ..this all happens in linked list.
But my problem is how to set the start and stop pointers pointing to each other initially.
Now, I never studied linked lists, but if it's just setting the pointers it's as simple as

1
2
start->next = stop;
stop->next = start;
Last edited on
> But I can't imagine a use for that.
Identifying an special node in a structure. By instance, the root on a tree.
Or a special condition. By instance, an empty list is the `header' node pointing to itself.

start->next = stop; however `start' is unititialized, so kaput.

@OP: you really should encapsulate the nodes. Create a `list' class to contain them.
1
2
3
4
5
class list{
private:
   struct node{/**/};
   node header;
};
well..i want that initially both these pointers point to each other.

then if i call an insert_node function, then the function will create a node and will insert it between start and stop


A pointer of type node* is either pointing at a node, or is null (pointing at nothing). To identify the special condition "the linked list is empty", you can either set both pointers to null or make them both point at some special placeholder node. They can't point to each other meaningfully.
Yes, they can.
1
2
3
bool list::empty() const{
   return start.next == stop.next;
}
(however the stop node seem pointless)
1
2
3
bool list::empty() const{
   return start.next == start;
}


Note that I'm using object instead of pointers for start and stop.
The advantage of this approach is that you could never dereference an invalid pointer, so you don't need to check for that in the operations.
Last edited on
@cubbi- ya , ur idea seems to be nice

@ne555- can u plz write the code in c, i dont know much about c++..and m wrking in c only..but plz write it bcoz i think u hv figured out an answer
Please write properly, google fails to translate that.

The equivalent in C would be
1
2
3
int list_empty(const list *this){
   return this->start.next == this->start;
}
Topic archived. No new replies allowed.