Pointers
Pointers
Advantages of Pointers:
data_type *ptr_name;
In this case , all different data type ,occupy the same amount of
space in memory but how much space they occupy will depend on
the platform where the code is going to run.
So, if we declared,
int x = 10;
int *ptr;
ptr = &x;
Memory representation:
Now illustrate this,
Assume that
int i = 3 ;
j = &i ;
int *j ;
This declaration tells the compiler that j will be used to store the
address of an integer value. In other words j points to an integer.
Note that printing the value of *( &i ) is same as printing the value
of i.
Example:-
main( )
{
int i = 3 ;
int *j ;
j = &i ; OUTPUT
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nAddress of i = %u", j ) ;
printf ( "\nAddress of j = %u", &j ) ;
printf ( "\nValue of j = %u", j ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", *( &i ) ) ;
printf ( "\nValue of i = %d", *j ) ;
}
Consider the following Statements :
int *p, x;
x =5;
p= &x;
Example:-
main( )
{
nt i = 3, *j, **k ;
j = &i ;
k = &j ; OUTPUT
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nAddress of i = %u", j ) ;
printf ( "\nAddress of i = %u", *k ) ;
printf ( "\nAddress of j = %u", &j ) ;
printf ( "\nAddress of j = %u", k ) ;
printf ( "\nAddress of k = %u", &k ) ;
printf ( "\nValue of j = %u", j ) ;
printf ( "\nValue of k = %u", k ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", * ( &i ) ) ;
printf ( "\nValue of i = %d", *j ) ;
printf ( "\nValue of i = %d", **k ) ;
}
ptr1 = &num1;
ptr2 = &num2;
This expression will increase the value of ptr, so that it now points to
the next memory location.
(*ptr) ++
NULL Pointers:
Null pointer which is a special pointer that does not point to any
value.
This means that a null pointer does not point to any valid memory
address.
GENERIC Pointer:
A generic pointer is a pointer variable that has void as its data type.
A void pointer or the generic pointer is a special type of pointer that
can be used to point to variables of any data type.
void *ptr;
int main()
{
int x=10;
char ch = ‘A’
void *gp;
gp = &x;
printf(“\n Generic pointer points to the integer value = %d”, *(int*) gp);
gp = &ch;
Printf(“\n Generic pointer now point to the character = %c”, *(char*) gp);
rerurn 0;
}
main( )
{
int a = 10, b = 20 ;
swapv ( a, b ) ;
printf ( "\na = %d b = %d", a, b ) ;
}
swapv ( int x, int y )
{
int t ;
t=x;
x=y;
y=t;
printf ( "\nx = %d y = %d", x, y ) ;
}
x = 20 y = 10
a = 10 b = 20
main( )
{
int a = 10, b = 20 ;
swapr ( &a, &b ) ;
printf ( "\na = %d b = %d", a, b ) ;
}
swapr( int *x, int *y )
{
int t ;
t = *x ;
*x = *y ;
*y = t ;
}
a = 20 b = 10
#include<stdio.h>
Pointer and Arrays:
The concept of array is very much bound to the one of the pointers.
An array occupies consecutive memory locations.
For example: int arr [ ] = {1, 2, 3, 4, 5};
1 2 3 4 5
arr[0] arr[1] arr[2] arr[3] arr[4]
2000 2002 2004 2006 2008
If arr and arr[0] points to location 2000. If we declare p as an integer
pointer, then we can make the pointer P to point to the array a by
following assignment,
P = arr;
We can access every value of array a by moving P from one element
to another.
i.e.,
P points to 0th element
P+1 points to 1st element, P+2 points to 2nd element, P+3 points to
3rd element, P +4 points to 4th element,
ptr
Array of Pointers(Pointer arrays):
Syntax:
datatype *arr_name[ size ];
Example:
int *ptr[5];
Here ptr is an array name whose elements are integer pointers
Program for Array of Pointers
#include<stdio.h>
int main( )
{
int a=10, b=20, c=30; int *ptr[3];
ptr[0]=&a;
ptr[1]=&b;
ptr[2]=&c;
printf(“Value of a : %d”,*ptr[0]);
printf(“Value of b : %d”,*ptr[1]);
printf(“Value of c : %d”,*ptr[2]);
return 0;
}
Strings and Pointers:
A pointer can be used to access the individual elements of a String.
For Example
char str[10];
declares a string str. The variable name of the string str holds the
address of the first element of the array i.e., it points at the starting
memory address.
So, we can create a character pointer ptr and store the address of
the string str variable in it. This way, ptr will point at the string str.
char *ptr= str;
In the following code we are assigning the address of the string str to
the pointer ptr.
#include<stdio.h>
int main()
{
char str[6] = "Hello"; // string variable
char *ptr = str; //pointer variable points to the first address of the string
// print the contents of the string using pointer
while(*ptr!='\0')
{
printf("%c", *ptr);
ptr++; // increment ptr so that it points to the next element of the string
}
return 0;
}