Can two pointer variables point to the same memory Address?
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
andprev
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.
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, 2022Comments
-
Sinchit Batham almost 2 years
If
p
andtemp
are two pointer variables wherep
containsNULL
andtemp
points to some memory address.Now suppose
p = temp;
That means now
p
points to same address as wheretemp
is pointing.Does that mean two pointer variables
p
andtemp
are now pointing to the same memory address?-
Giorgi Moniava over 7 yearsyes, it does, that is also one of the use cases for pointers.
-
harold over 7 yearsThis is like asking "can two ints have the same value?"
-
chqrlie over 7 years@harold: which is a trick question for non 2s complement architectures ;-)
-
Sinchit Batham over 7 yearsAs 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 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 over 7 yearsThe answer to your question "Does that mean two pointer variables
p
andtemp
are now pointing to the same memory address?". is "That means nowp
points to same address as wheretemp
is pointing."
-
-
Tom Taylor over 7 yearsI think this is possible only with void pointers. Is that so?
-
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 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 over 7 yearsWhen 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 over 7 yearsReferring value from an unknown memory location would throw "Segmentaion fault" error.
-
chqrlie over 7 yearsDereferencing 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 almost 5 yearsto correct the verbage, a pointer isn't an object, it's a variable.. it's different.. but the answer is spot on
-
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 almost 5 yearsah 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.