Can two pointer variables point to the same memory Address?

18,355

Solution 1

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

Solution 2

Yes it does! Multiple pointers can point to the same thing.

Share:
18,355
Sinchit Batham
Author by

Sinchit Batham

i am currently pursuing b.tech in computer Science and engineering at Kamla Nehru Institute of technology and in future want to be a software Developer Interested in C, C++ and java

Updated on June 21, 2022

Comments

  • Sinchit Batham
    Sinchit Batham almost 2 years

    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?

    • Giorgi Moniava
      Giorgi Moniava over 7 years
      yes, it does, that is also one of the use cases for pointers.
    • harold
      harold over 7 years
      This is like asking "can two ints have the same value?"
    • chqrlie
      chqrlie over 7 years
      @harold: which is a trick question for non 2s complement architectures ;-)
    • Sinchit Batham
      Sinchit Batham over 7 years
      As far as I know Int is a data type , where pointer is a pointer not a data type, so ints variable may have the same value but not sure about pointers
    • chqrlie
      chqrlie over 7 years
      @SinchitBatham: char* is a data type too. Multiple instances can have the same contents. The contents of a pointer is the address of an object. Nothing prevents you from storing that into multiple pointers of the appropriate type.
    • StoryTeller - Unslander Monica
      StoryTeller - Unslander Monica over 7 years
      The answer to your question "Does that mean two pointer variables p and temp are now pointing to the same memory address?". is "That means now p points to same address as where temp is pointing."
  • Tom Taylor
    Tom Taylor over 7 years
    I think this is possible only with void pointers. Is that so?
  • chqrlie
    chqrlie over 7 years
    *pointer2 is not just some junk value, dereferencing an uninitialized pointer invokes undefined behavior, which means the program can fail in various unexpected ways.
  • chqrlie
    chqrlie over 7 years
    @RajasubaSubramanian: yes, but you have a similar situation with list links: struct list { struct list *car, *cdr; } list = { &list, NULL ];
  • Tom Taylor
    Tom Taylor over 7 years
    When pointer2 is initialized it would be allotted some memory. We do not know what value the memory location has (which i referred as junk value). When we try to refer value in the pointer2 location - the compiler thinks it as a address value and try to fetch value from that location - which may or might not be present.
  • Tom Taylor
    Tom Taylor over 7 years
    Referring value from an unknown memory location would throw "Segmentaion fault" error.
  • chqrlie
    chqrlie over 7 years
    Dereferencing an uninitialized pointer invokes undefined behavior, which may throw a Segmentation fault in many cases, but will not on some older and simpler architectures. Do not rely on such side effects.
  • sksallaj
    sksallaj almost 5 years
    to correct the verbage, a pointer isn't an object, it's a variable.. it's different.. but the answer is spot on
  • chqrlie
    chqrlie almost 5 years
    @sksallaj: variable is not the exact term, object is, but it is confusing for people who do not yet grap the concept of pointer. I shall amend the answer to avoid this confusion.
  • sksallaj
    sksallaj almost 5 years
    ah gotcha, in recent posts about it, they referred to them as variables because of the representation of the pointer, so that sent me mixed signals, but after looking up solid resources, you're right. Thanks for clarifying.