Dereferencing Pointer
Dereferencing Pointer
http://stackoverow.com/questions/4955198/what...
sign up
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no
registration required.
log in
tour
help
careers 2.0
What does it mean to dereference a pointer? Can I please get a explanation with an example?
c
pointers
dereference
this can help you: stackoverflow.com/questions/2795575/ Harry Joy Feb 10 '11 at 9:17
int *p; would define a pointer to an integer, and *p would dereference that pointer, meaning that it would
actually retrieve the data that p points to. Peyman Feb 10 '11 at 9:21
Binky's Pointer Fun (cslibrary.stanford.edu/104) is a GREAT video about pointers that might clarify things.
@Erik- You rock for putting up the Stanford CS Library link. There are so many goodies there...
templatetypedef Feb 10 '11 at 9:27
Harry's response is the opposite of helpful here. Jim Balter Feb 10 '11 at 9:39
5 Answers
A pointer scenario
Consider in C, given a pointer such as p below...
const char* p = "abc";
...four bytes with the numerical values used to encode the letters 'a', 'b', 'c', and a 0 byte to denote the end of
the textual data, are stored somewhere in memory and the numerical address of that data is stored in p .
For example, if the string literal happened to be at address 0x1000 and p a 32-bit pointer at 0x2000, the
memory content would be:
Memory Address (hex)
1 of 5
Variable name
Contents
04/04/2014 03:35 PM
http://stackoverow.com/questions/4955198/what...
'a' == 97 (ASCII)
'b' == 98
'c' == 99
0
p
1000 hex
Note that there is no variable name/identifier for address 0x1000, but we can indirectly refer to the string
literal using a pointer storing its address: p .
2 of 5
04/04/2014 03:35 PM
http://stackoverow.com/questions/4955198/what...
In C++, memory allocation is normally done with the new operator, and deallocation with delete :
int* p = new int(10);
delete p;
p = new int[10];
delete[] p;
p = new int[10]();
delete[] p;
community wiki
14 revs, 2 users 99%
Tony D
Appreciate the way you explained it..... Thanks for sharing pragati May 27 '13 at 9:12
is p[1] and *(p + 1) identical? That is, Does p[1]
Pacerier Sep 22 '13 at 18:20
@Pacerier: from 6.5.2.1/2 in the N1570 draft C Standard (first I found online) "The definition of the subscript
operator [] is that E1[E2] is identical to (*((E1)+(E2)))." - I can't imagine any reason why a compiler wouldn't
immediately convert them to identical representations in an early stage of compilation, applying the same
optimisations after that, but I don't see how anyone can definitely prove the code would be identical without
surveying every compiler ever written. Tony D Sep 23 '13 at 6:51
@TonyD, no I'm not talking about compiler implementations, but rather, did the official standard guarantees that
behavior? I mean the latest standards C90, C99, and C11.... Pacerier Sep 24 '13 at 4:13
@Pacerier: the Standards are framed in terms of necessary observable behaviours, but how the compilers
choose to map source code to machine opcodes is not mandated. Tony D Sep 26 '13 at 14:37
Dereferencing a pointer means getting the value that is stored in the memory location pointed by the pointer.
The operator * is used to do this, and is called the dereferencing operator.
int a = 10;
int* ptr = &a;
printf("%d", *ptr); // With *ptr I'm dereferencing the pointer.
// Which means, I am asking the value pointed at by the pointer.
3 of 5
04/04/2014 03:35 PM
http://stackoverow.com/questions/4955198/what...
// Now a's content is no longer 10, and has been modified to 20.
edited Jan 29 at 16:15
Migol
2,868 6 24 48
A pointer doesn't point to a value, it points to an object. Keith Thompson Apr 30 '13 at 5:04
@KeithThompson A pointer does not point to an object, it points to a memory address, where an object
(maybe a primitive) is located. mg30rg Mar 6 at 14:26
@mg30rg: I'm not sure what distinction you're making. A pointer value is an address. An object, by definition, is
a "region of data storage in the execution environment, the contents of which can represent values". And what
do you mean by "primitive"? The C standard doesn't use that term. Keith Thompson Mar 6 at 16:34
@KeithThompson I was barely pointing out, that you did not actually add value to the answer, you were only
nitpicking on terminology (and did that wrong also). The pointer value surely is an address, that's how it
"points" to a memory address. The word "object" in our OOPdriven world can be misleading, because it can be
interpreted as "class instance" (yep, I was not aware that the question is labeled [C] and not [C++]), and I used
the word "primitive" as in the opposite of "copmlex" (data structure like a struct or class). mg30rg Mar 7 at
8:26
@mg30rg: I stand by my original statement: A pointer doesn't point to a value, it points to an object. And the
question was updated to correct that. Terminology is important, and no, I don't believe I got anything wrong.
Incidentally, the C++ standard's definition of the word "object" is very similar to C's, and has nothing to do with
OOP. By "primitive", it sounds like mean what the standard calls "scalar"; yes the object pointed to by a pointer
may or may not be of scalar type, but I'm not sure that adds anything. Keith Thompson Mar 7 at 15:59
A pointer is a "reference" to a value.. much like a library call number is a reference to a book.
"Dereferencing" the call number is physically going through and retrieving that book.
int a=4 ;
int *pA = &a ;
printf( "The REFERENCE/call number for the variable `a` is %p\n", pA ) ;
// The * causes pA to DEREFERENCE...
4 of 5
04/04/2014 03:35 PM
http://stackoverow.com/questions/4955198/what...
In Simple words, dereferencing means accessing the value from certain Memory location against which that
pointer is pointing.
answered Dec 30 '13 at 7:09
Fahad Naeem
30 6
Not the answer you're looking for? Browse other questions tagged c pointers
dereference or ask your own question.
5 of 5
04/04/2014 03:35 PM