Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can two pointer variables point to the same memory Address?

If p and temp are two pointer variables where p contains NULL and temp points to some memory address.

Now suppose p = temp;

That means now p points to same address as where temp is pointing.

Does that mean two pointer variables p and temp are now pointing to the same memory address?

like image 328
Sinchit Batham Avatar asked Dec 04 '16 14:12

Sinchit Batham


1 Answers

Yes, two pointer variables can point to the same object:

Pointers are variables whose value is the address of a C object, or the null pointer.

  • multiple pointers can point to the same object:

    char *p, *q;
    p = q = "a";
    
  • a pointer can even point to itself:

    void *p;
    p = &p;
    
  • here is another example with a doubly linked circular list with a single element: the next and prev links both point to the same location, the structure itself:

    struct dlist {
        struct dlist *prev, *next;
        int value;
    } list = { &list, &list, 0 };
    
like image 186
chqrlie Avatar answered Nov 28 '22 21:11

chqrlie