Pointers – Introduction
Pointer is a variable that holds the address of another variable.
Consider the following statement :
Int qty = 179;
The representation of the variable in memory is as follows
It means ‘p’ is a pointer variable that holds the address of another integer variable/
Example:
int qty = 175;
int *p;
p= &qty;
eg:
‘*’ can be treated as value at address
P = &
n = *p; longleftrightarrow
Program:
main ( )
left { right.
int x,y
int *p;
clrscr ( );
x= 10;
p = &
y= *p;
printf (‘Value of x = %d
printf (‘x is stored at address %un”, &
printf (‘Value of using pointer = %d
printf (‘address of x using pointer = %u
printf (“value of x in y = %d
*p = 25;
printf (“now x = %d
getch ( );
left. right }
Output:
Value of x = 10
x is stored at address = 5000
Value of using pointer = 10
Address of x using pointer = 5000
Value of x in y = 10
Now x = 25
Arithmetic operations using pointers:
Like any other variables, pointer variables can be used in expressions. For eg. If p1 and p2 are properly declared and initialized then the following statements are valid
a) *p1 + *p2
b) *p1 - *p2
c) *p1 * *p2
d) *p1 / *p2 Note : There must be a blank space between / and * otherwise it is
e ) p1 + 4
h) p1++
i) – – p2
j) sum + = *p2
j) p1 > p2
k) p1 = = p2
l) p1 ! = p2
Note : Comparisons can be used meaningfully in handling arrays and strings
The following statements are invalid
a) p1 + p2
b) p1 * p2
c) p1 / 3
d) p1 / p2
Program:
Output:
Address of a = 1234
Address of b = 5678
a = 12 b= 4
x = 42 y= 9
0 Doubts's