Академический Документы
Профессиональный Документы
Культура Документы
POINTERS
10.1 INTRODUCTION
A pointer provides a way of accessing a variable without referring to the variable
directly. It provides a symbolic way of using addresses. A pointer is a data object that
refers to a memory location which is an address of another variable. Thus, a pointer
variable may contain address of another variable or any valid address in the computer
memory. A pointer provides an indirect way of accessing the value of a data item.
Pointers are used in the some situations, and few of the of the reasons where
pointers can be used are
data_type *var1,*var2...*var n;
Where data_type is any valid data type and var1, var2.var n are the names of the
pointer variables. The declaration tells the compilers that var1, var2.var n are used to
store the addresses of values. The data_type of the pointer defines what type of
variables the pointer can point to. Each and every pointer variable has allocated 2 bytes
of memory.
Examples:int *a; //a is a pointer variable of integer type which points to address of an int value
float *f; // f is a pointer variable of float type, which points to address of a float value
char *c; // c is a pointer variable of char type, which points to address of a char value
1)
main()
{
int *a,b;
b=5;
a=&b;
printf(%d,b);
printf(%d,*b);
printf(%d,&b);
}
2)
Dynamic memory allocation with respect to pointer:In this dynamic memory allocation with respect to pointers the memory was
allocated and destroyed using 4 library functions.
1) malloc()
2) calloc()
3) realloc()
4) free()
1) malloc ():The function is used for memory allocation at the time of execution. This function
takes only one argument and that argument represents no. of bytes to be allocated.
The malloc() function returns the base address of the memory which it is
allocated and return type of address is of type void. Thats why we have to typecast that
void type to any other primitive data type depending on the pointer variable.
The memory which allocated by malloc() function by defaultly takes the large
value.
Format:- (void*)malloc(no. of bytes);
Ex:- float *p;
P=(float *)malloc(sizeof(float));
Here p allocates 2 bytes, malloc() returns 4 bytes base address of 4 bytes of void
type and it is typecasted to float type.
4 bytes
1024
Ex:-
1024
int *p;
P=(int*)malloc(5*(sizeof(int));
1024
1026
1028
1030
1032
1024
Calloc():Format:- (void*)calloc(no.of objects,bytes of each datatype);
Ex:-
int *p;
P=(int*)calloc(10,sizeof(int));
1024 1026
1024
..
realloc():This function is capable of increasing or decreasing the space that has been allocated
previously.
Format:- (void*)realloc(pointer variable,no of bytes);
The first argument of realloc() is a pointer to a block of memory of which the size
is to be alter.
The second argument specifies the new size.
If the allocation is successful the returned value is again the pointer variable to
the first byte of the allocated memory retaining the old contents.
Ex:-
int *p;
P=(int*)malloc(10*sizeof(int));
P=(int*)realloc(p,10);
free():the memory obtained by malloc() , calloc() and realloc() functions can be freed
when that space is not required for any storage. The function free() dois this job.
Ex:
free(pointer variable);
int *p,*q;
p=(int*)malloc(sizeof(int));
q=(int*)malloc(sizeof(int));
printf(enter a,b values);
scanf(%d %d,p,q);
printf(sum is :%d,*p+*q);
free(p);
}
Call by value:void swap(int , int );
main()
{
int *p,*q;
p=(int*)malloc(sizeof(int));
q=(int*)malloc(sizeof(int));
printf(enter a,b values);
scanf(%d %d,p,q);
//5
10
printf(\n Before swapping: a= %d \t b= %d,a,b); // a=5 b=10
swap(a,b);
printf(\n After swapping: a= %d \t b= %d,a,b); // a=5 b=10
free(p); free(q);
}
void swap(int x, int y)
{
int t=x;
x=y;
y=t;
}
int t=x;
x=y;
y=t;
}
The arguments can generally passed to functions in one of the two ways:
1) sending the values of the arguments.
2) sending the address of the arguments.
In the first method, the valud of each actual argument in the calling function is
copied into the corresponding formal arguments of the called function. With this method
changes are made to the formal arguments in the called function and have no effect on
the values of the actual arguments.
In the second method the addresses of actual arguments in the calling function
are copied into the formal arguments of the called function. This means that, using these
address we would have an access to the actual arguments from the formal arguments
and hence we would able to manipulate the actual arguments in the function definition.
Using call by reference we can make a function return more than one value at a
time, which is not possible in call by value.
If a function is called by passing the values then such functions are called call by
value. By this, what we mean is on calling a function ,we are passing values of variables
to it.
If we pass the location no.(address) of a variable to a function, then it is called
call by reference i..e.., we are passing addresses of variables to the called functions. In
called function, we should declare the pointer variable of the same datatype in order to
receive the address of the variable.
Pointer with 1-D Arrays:Address = base address + no. of elements * scale factor
Scale factor is nothing but no. of bytes allocated based on data type depending
on that address.
1) program to read 1-D array & print that array
main()
{
int a[5],n,i;
printf(enter n value:);
scanf(%d,&n);
for(i=0;i<n;i++) scanf(%d,a+i);
printf( values r:);
for(i=0;i<n;i++) printf(%d,*(a+i));
}
2) program to read 1-D array & print that array
main()
{
int *p,n,i;
printf(enter n value:);
scanf(%d,&n);
p=(int*)malloc(n*sizeof(int));
printf(enter %d values:,n);
for(i=0;i<n;i++) scanf(%d,p+i);
printf( values r:);
for(i=0;i<n;i++) printf(%d,*(p+i));
}
3) program to perform linear search
main()
{
int *p,n,i;
printf(enter n value:);
scanf(%d,&n);
p=(int*)malloc(n*sizeof(int));
read(p,n);
printf( values r:);
for(i=0;i<n;i++) printf(%d,*(p+i));
search(p,n);
}
void read(int *p, int n)
{
int i;
printf(enter %d values:,n);
for(i=0;i<n;i++) scanf(%d,p+i);
}
void search(int *q,int n)
{
int k;
for(i=0;i<n;i++)
if(*(q+i)= = k)
{
printf( element found);
break;
}
printf( element not found);
}
4) program to perform binary search
main()
{
int *p,n,i;
printf(enter n value:);
scanf(%d,&n);
p=(int*)malloc(n*sizeof(int));
read(p,n);
printf( values r:);
print(p,n);
search(p,n);
}
void read(int *p, int n)
{
int i;
printf(enter %d values:,n);
for(i=0;i<n;i++) scanf(%d,p+i);
}
void print(int *p, int n)
{
int i;
printf( %d values:,n);
for(i=0;i<n;i++) printf(%d,p+i);
}
void sort(int *q,int n)
{
int k,i,j,t;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(*(q+j+1) < *(q+j))
{
t=*(q+j+1);
*(q+j+1)= *(q+j);
*(q+j)= t;
}
}
Pointer with 2-D arrays:5) program to read 2-D array & print them
main()
{
int a[5][5];
for(i=0;i<5;i++)
for(j=0;j<5;j++)
scanf( %d,*(a+i)+j);
for(i=0;i<5;i++)
for(j=0;j<5;j++)
printf( %d,*(*(a+i)+j));
}
6)
main()
{
int *p,n,m;
printf( enter n,m values:);
scanf( %d %d,&m,&n);
p=(int*)malloc(m*n*sizeof(int));
for(i=0;i<5;i++)
for(j=0;j<5;j++)
scanf( %d,*(p+i*n)+j);
for(i=0;i<5;i++)
for(j=0;j<5;j++)
printf( %d,*(*(p+i*n)+j));
}
ARRAY OF POINTERS:Whenever addresses are stored as array elements such an array is called an array
of pointers. The format for declaration of an array of pointers is
data type * array name[size];
(or)
data type * (array name[size]);
ex:-
1)
2)
int *a[5];
main()
{
float *a[5],f1,f2;
f1=10.24;
f2=12.54;
a[0]=&f1;
a[1]=&f2;
}
3)
int *y[15];
int x[15][15];
y[0]=x;
y[1]=x[1];
.
y[14]=x[14];
The size of array of pointers in the declaration represents the no. of rows in a 2-D
array.
3)
program to read a 2-D array & print that array using array of pointers
main()
{
int *p[3],n,m,i,j;
printf( enter n,m values:);
scanf( %d %d,&m,&n);
for(i=0;i<m;i++)
p[i]=(int*)malloc(n*sizeof(int));
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf( %d,p[i]+j);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf( %d,*p[i]+j));
}
4)program to perform addition operation on matrices
main()
{
int *a[3],*b[3],*c[3],p,q,n,m,i,j;
printf( enter n,m values:);
scanf( %d %d,&m,&n);
for(i=0;i<m;i++)
a[i]=(int*)malloc(n*sizeof(int));
printf( enter p,q values:);
scanf( %d %d,&p,&q);
for(i=0;i<p;i++)
b[i]=(int*)malloc(q*sizeof(int));
if(m= =p && n= =q)
{
read(a,m,n);
print(b,p,q);
add(a,b,c,m,n);
print(c,m,n);
}
else
printf( addition is not possible);
}
void read(int *a[10],int m,int n)
{
printf( enter %d elements:,m*n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf( %d,a[i]+j);
}
void print(int *a[10],int m,int n)
{
printf( %d elements r:,m*n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf( %d,*(a[i]+j));
}
void add(int *a[10],int *b[10],int *c[10],int m,int n)
{
for(i=0;i<m;i++)
c[i]=(int*)malloc(n*sizeof(int));
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
*(c[i]+j)=0;
*(c[i]+j)= *(a[i]+j) + *(b[i]+j);
}
}
5)program to perform substraction operation on matrices
main()
{
int *a[3],*b[3],*c[3],p,q,n,m,i,j;
printf( enter n,m values:);
scanf( %d %d,&m,&n);
for(i=0;i<m;i++)
a[i]=(int*)malloc(n*sizeof(int));
printf( enter p,q values:);
scanf( %d %d,&p,&q);
for(i=0;i<p;i++)
b[i]=(int*)malloc(q*sizeof(int));
if(m= =p && n= =q)
{
read(a,m,n);
print(b,p,q);
sub(a,b,c,m,n);
print(c,m,n);
}
else
printf( addition is not possible);
}
void read(int *a[10],int m,int n)
{
printf( enter %d elements:,m*n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf( %d,a[i]+j);
}
void print(int *a[10],int m,int n)
{
printf( %d elements r:,m*n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf( %d,*(a[i]+j));
}
void sub(int *a[10],int *b[10],int *c[10],int m,int n)
{
for(i=0;i<m;i++)
c[i]=(int*)malloc(n*sizeof(int));
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
*(c[i]+j)=0;
}
}
POINTER TO ARRAY:Array elements are stored in contiguous memory locations 2-D arrays are stored
row by row. In c, it is possible to define a 2-D array as a pointer to a group of
consecutive 1-D array as
Data type ( * array_name)[size];
It is important to place the parenthesis sourrounding the arrayname & preceeding
asterisk in the declaration.
Ex:main()
{
int x[3][5];
int (*a)[5];
a=x;
}
1024
1034
1044
The statement a=x is specifier that a[0] points to the zeroth row of x depending
on column size 5 only.
7) program to read a 2-D array & print that array using pointer to array
main()
{
int (*a)[3],x[3][5],n,m,i,j;
printf( enter n,m values:);
scanf( %d %d,&m,&n);
a=(int*)malloc(m*n*sizeof(int));
printf( enter %d elements:,m*n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(%d,*(a+i)+j);
printf( elements r:);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf(%d,*(*(a+i)+j));
}