Pointers
- Pointers are a type of variables in C
- A pointer variable can hold only memory addresses ofvariables
- By using pointers we are directly dealing with the memory addresses.
int *ptr ;
ptr is a pointer to an integer .It holds the address of an integer (a 2 byte location )
float *x ; /* x is a pointer to a float */
char *p ; /* p is a pointer to a char */
Making a pointer point to a variable :-
A pointer variable must be assigned an address of a variable Then only it will point to that variable .Otherwise it is called as a dangling pointer holding some (garbage) address
main
{
int *p,x ;
/* p is a pointer to an integer. P can hold address of an integer */
x = 30 ;
p = &x; /* p now points to x */
printf( “ p = %u &x = %u “, p , &x);
printf(“ \n x = %d values at p = %d “ , x , *p);
}
Pointer operators :-
& Address of variable
* Indirection or dereferencing operator
It is used to access the value at an address. It can only be used with pointers.
Note : Key words near and far
A near pointer (default) variable is allocated 2 bytes which means it can be used to access the first 64k of memory
int near *p is the same as int *p
A far poiner is allocated 4 bytes which means it can be used to access beyond the 64k memory.
char far *p ;
Program:
main( )
{
char *ptr, ch;
ptr = &ch;
clrscr( );
ch = ‘A’;
printf(“ *ptr = %c “, *ptr); // output is A
*ptr + = 3;
printf( “ \n ch = %c “ , ch ); // output is D
getch( );
}
Importance of pointers :-
Functions can return only a single value but with pointers we can have a function changing a number of values and making these changes visible to the calling function
Passing values to functions can be by value or by reference
void swap(int x, int y);
void swap (int x, int y)
{
int t ;
t = x ;
x = y ;
y = t ;
}
main( )
{
int a = 20 ,b = 40 ;
printf(“ Before swap a = %d b = %d \n “ , a , b ) ;
swap ( a , b ) ;
printf(“ \n After swap a = %d , b = %d “ , a , b );
}
Output is 20, 40 in both the cases before and after swap is called .
Note :
Pass by value means when we pass values in function calls the original variables do not get affected. The values are used for some calculation. This is safe as the original values are intact.
Pass by reference:
void swap (int *x, int *y);
void swap (int *x, int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
main( )
{
int a = 20 ,b = 40 ;
printf(“ Before swap a = %d b = %d \n “ , a , b ) ; /* 20, 40 */
swap ( &a , &b ) ;
printf(“ \n After swap a = %d , b = %d “ , a , b ); /* 40,20 */
}
Note :
In pass by reference the pointers are passed and not the values. If the function changes the values in these locations then even if you don’t return the values these changes are visible to the calling function. When we pass an address of a variable instead of its value to functions the function can permanently alter the value of the variable.
Program:
void change ( int x); / address is passed */
void change ( int *x)
{
int i;
for( i = 0 ; i <>
*( x + i ) += 10 ; /* Values of 4 elements increase by 10 */
}
Operations on Pointers
As pointers hold addresses the operations on pointers are limited to meaningful operations on addresses as follows :-
1.Pointer arithmetic is sensitive to the data type and the memory allocated for those data types
2.An integer can be added to or subtracted from a pointer . When an integer n is added to a pointer we move to a new location ( n x sizeof(data type) ) bytes away from the original location
For e.g
Suppose we have an int *ptr with a value of 4000 then ptr + 3 will give an address 4006 assuming an int occupies 2 bytes
Suppose we have a float *ptr with a value of 4000 then ptr + 3 will give an address 4012 assuming a float occupies 4 bytes
ptr += 3 ; /* Look !! The original address is lost */
ptr -= 4 ;
The addition is commutative a+ 3 is the same as 3 + a
3. Pointers can be incremented or decremented . The value increases by (1 x sizeof(data type ) )
For e.g suppose we have a float *ptr and the value of ptr is 62000 then ptr++ will take us to a new location with address 62004 Remember here the original address is lost and we have moved away .
4. One Pointer can be assigned the value of another pointer. It then points to the same location
For e.g int *p , *q , a ;
p = &a ; /* p points to variable a */
q = p ; /* pointer assignment */
/* q now also points to variabe a */
5. Pointers can be compared using > < >= <= == or !=
6.Two Pointers pointing to the same data type can be subtracted meaningfully
Multiple indirection :- Pointer to a pointer
As a pointer is a type of variable, it is also stored in some memory location and has an address. We can have a variable holding the address of a pointer variable. Such a variable is called as a pointer to a pointer and the concept is termed multiple indirection.
int **p ;
In this declaration p is a pointer to an int pointer
Program:-
void main( )
{
int p = 20 ;
int *ptr = &p ;
int **pptr = &ptr ;
printf(“%d %d %d” , p , *ptr , **pptr );
/* prints 20 20 20*/
}
Application of multiple indirection:-
Arrays and pointers :-
Arrays are closely related to pointers. An array name is a pointer to the starting address of the array.
int arr[5] = { 1 , 3 , 5 , 7 , 9 };
Here the array name arr is an address . It is the address of the first element of the array . Arr is the same as &arr[0]
main( )
{
int arr[5] = { 1 , 3 , 5 , 7 , 9 } , *ptr , j ;
ptr = arr ; /* arr is an address */
clrscr( );
for( j = 0 ; j < style=""> j++ )
printf ( “ %d %d %d %d %d %d %d\n“ , a[ j ] , * (arr + j ) , * (ptr + j ) , * ( j + ptr ) , ptr[ j ] , j [ptr] , j [arr] );
}
Output : 1 1 1 1 1 1 1
3 3 3 3 3 3 3
and so on
Note
*(arr + j ) and *( j + arr ) are both interpreted as value at address (arr + j )
*(ptr + j ) and *( j + ptr ) are interpreted as value at address (ptr + j )
[ ] operator:- Meaning of arr [ j ] , j [ arr ] , ptr [ j ] , j [ ptr ] :-
arr[ j ] translates as value at address (arr + j )
Thus ptr[ j ] is the same as j [ ptr ] as pointer (int addition is commutative)
J[arr] is the same as arr[j]
Meaning of of a negative array index arr[-4] : Can an array have a negative index ? Yes . In C arr[-4] is intepreted as value at address (arr – 4 ) i.e., *( arr – 4 )
There is no array bound checking in C . All array operations are address manipulations
An array name is a const pointer
Though an array name is a pointer, it cannot be incremented or decremented. It is not a variable
main( )
{
int a[5] = { 10 , 20, 30, 40 };
change (a); /* An array name is an address */
clrscr( );
for( i = 0 ; i < style=""> i++)
printf(“ %d “ , a[I]);
}
No comments:
Post a Comment