6

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?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Sinchit Batham
  • 103
  • 1
  • 2
  • 10
  • 1
    yes, it does, that is also one of the use cases for pointers. – Giorgi Moniava Dec 04 '16 at 14:17
  • 3
    This is like asking "can two ints have the same value?" – harold Dec 04 '16 at 14:18
  • @harold: which is a trick question for non 2s complement architectures ;-) – chqrlie Dec 04 '16 at 14:21
  • 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 – Sinchit Batham Dec 04 '16 at 14:22
  • @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. – chqrlie Dec 04 '16 at 14:24
  • 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."* – StoryTeller - Unslander Monica Dec 04 '16 at 14:27

3 Answers3

9

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 };
    
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • I think this is possible only with void pointers. Is that so? – Tom Taylor Dec 04 '16 at 14:25
  • 1
    @RajasubaSubramanian: yes, but you have a similar situation with list links: `struct list { struct list *car, *cdr; } list = { &list, NULL ];` – chqrlie Dec 04 '16 at 14:29
  • to correct the verbage, a pointer isn't an object, it's a variable.. it's different.. but the answer is spot on – sksallaj Jul 22 '19 at 17:06
  • 1
    @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. – chqrlie Jul 23 '19 at 10:52
  • 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. – sksallaj Jul 23 '19 at 15:14
2

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

Jack Deeth
  • 3,062
  • 3
  • 24
  • 39
0

Yes two pointer variable can point to the same memory address.

As we know that pointer is a variable which contains address of same data type. Consider the following example in C

#include<stdio.h>

int main() {
int a,*pointer1;
a = 10;
pointer1 = &a;   
int *pointer2;

printf("\n Val : %d",*pointer1);    //This contains address of the variable a - so accessing it with * prints its value - value of “a"
printf("\n Val : %d", *pointer2);   //This contains some junk value - so accessing it with * causes segmentation fault - since there might not be memory address with that junk value       

pointer2 = pointer1;                //Now pointer1 reference i.e.; address value stored in pointer1 is made a copy and now both pointer1 and pointer2 will point to the same memory location
printf("\n Val : %d", *pointer2);   //Now this would print the value of “a"

return -1;
}

The same applies to linked list address. Your variable “p” and “temp” would point to the same memory address now !

Tom Taylor
  • 3,344
  • 2
  • 38
  • 63
  • `*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 Dec 04 '16 at 14:26
  • 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 Dec 04 '16 at 14:30
  • Referring value from an unknown memory location would throw "Segmentaion fault" error. – Tom Taylor Dec 04 '16 at 14:30
  • 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. – chqrlie Dec 04 '16 at 14:33