You are on page 1of 10

POINTER

Pointer
• Definition:
A pointer is a variable whose value is the address of another
variable.

• Pointers are said to "point to" the variable whose address


they store.
• The value of a pointer variable is an address, i.e value refers
the address of the another memory space.
• The data is typically stored in this memory space. Therefore,
when you declare a pointer variable, you also specify the data
type of the value to be stored in the memory location pointed
to by the pointer variable.
Pointer
• In C++, the pointer is declared by using the asterisk symbol
(*) between the data type and the variable name.
• Syntax:
dataType *identifier;
• Example:
int *p;
char *ch;
• Here, both p and ch are the pointer variables.
• The content of p points to a memory location of type int and
the content of ch points to a memory location of type char.
• Typically, p is called a pointer of type int and ch is called the
pointer of type char.
• The statement
int *p;
is equivalent to the statement
int* p;
which is also equivalent to the statement
int * p;
• Thus the asterisk (*) can appear anywhere between the data
type and the variable name.
• The statement
int *p, q;
here, p is the pointer of type int and q is a variable of type int.
• In the statement
int *p, *q;
Both are pointers of type int.
Address-of operator (&)
• In C++, the ampersand &, called the address of operator.
• It is a unary operator.
• This return the address of its operand.
• Example:
addr = &mvar;
This would assign the address of variable mvar to addr.
mvar

addr
Dereference operator (*)

• Pointers can be used to access the variable


they point to directly.
• This is done by preceding the pointer name
with the dereference operator (*).
• The operator itself can be read as "value
pointed to by".
• Example:
int *p;
int var, newvar;
var = 25;
p = &var;
newvar = *p;
p

newvar

This could be read as: “newvar equal to value pointed to by p", and the
statement would actually assign the value 25 to newvar, since p is 1776, and
the value pointed to by 1776 (following the example above) would be 25.
• Example:
int *p;
int var, newvar, newvar1;
var = 25;
p = &var;
newvar1 = p;
newvar = *p;

• It is important to clearly differentiate that p refers to the


value 1776, while *p (with an asterisk * preceding the identifier)
refers to the value stored at address 1776, which in this case is 25.
• Notice the difference of including or not including the dereference
operator (I have added an explanatory comment of how each of
these two expressions could be read)
• newvar1 = p; (newvar1 is equal to p (1772), address of var)
newvar = *p; (newvar is equal to value pointed to by p (25) )
int a = 23, b = 40;
int *p, *q;
p = &a, q = &b;
cout<<"address of a is "<<&a<<endl;
cout<<"address of b is "<<&b<<endl;
cout<<p<<" "<<q<<endl;
*q = 20; B = 40
B = 20
b = *p+a; B = *p+a = 23 + 23 = 46

cout<<b<<endl<<*q<<endl;
int x, y;
int *p = &x, *q = &y;
*p = 35;
*q = 98;
*p = *q;
Cout<<x<<“ “<<y<<endl;
Cout<<*p<<“ “<<*q<<endl;

You might also like