Click here to Skip to main content
16,021,181 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can anybody provide a example to understand the how to reference and dereference a pointer
Posted

You dereference apointer when use the * operator to access pointed address value, e.g.
C
int a,b;
int * p; // this just declares p as pointer to integer

a = 5;

p = &a; // now p contains the address of the a

b = *p; // here you are dereferencing the pointer p

*p = 7; // here too you are dereferencing the pointer p
 
Share this answer
 
 
Share this answer
 
Comments
mvigsnhwar 18-Jan-13 5:21am    
Thnk you Aushkar , CPallini , OriginalGriff for ur support
To reference and dereference a pointer basically mean "use the pointer".

You use a Reference operator when you want to assign a value to a pointer:
C++
int  i;
int* pi;

i = 666;
pi = &i;
Here, you declare an integer and a pointer to an integer, you assign a value to the integer, and then you use the reference operator "&" to get the address of the integer and assign it to the pointer.

You use a Dereference operator when you use the pointer to get the value it is pointing at:
C#
int  i, j;
int* pi;

i = 666;
pi = &i;
j = *pi;
Here we add another integer to the variables, then you use teh dereference opertaor "*" to fetch the value from where pi is pointing and assign it to j. At the end, i and j both contain the same value: 666

[edit]Typo "you you uthe" changed to "you use the" - OriginalGriff[/edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900