Unit Iv
Unit Iv
UNIT IV
POINTERS
SAMPLE QUESTIONS
Pointer
1) Definition:- Pointer means which is pointing to other.
Pointer Variable:-The variable which stores the address of another variable is called
“Pointer Variable”.
Pointer variable declaration:-
Syntax:-data-type *var1,*var2,........,var *n;
Example:- int *x,*y;
Initialization of pointer variable:-
int a,b,*x,*y;
x=&a;
y=&b;
Here the symbol ‘&’ is used for the purpose of storing the address into the pointer variable.
3) Pointers to pointers
The variable which stores the address of pointer variable is called ‘pointer to pointer
variable’.
Syntax:-data-type **var1,**var2,........,var **n;
Example :- int **x,**y;
Consider the following program to display the variable value using pointer-to-pointer
concept.
#include<stdio.h>
#include<conio.h> Output
void main() a= 5
{ a= 5
int a,*x,**y; a= 5
clrscr();
a=5;
x=&a;
y=&x;
printf(" a =%d\n",a);
printf(" a =%d\n",*x);
printf(" a =%d",**y);
getch();
}
4) Pointers and function arguments
1. In this the actual parameters address are passed to the formal parameters.
2. The modifications to the formal parameters will automatically reflect the actual
parameter.
3. Pointers can return the more than one value from the called function.
Write a C program for swapping of two numbers using pointers.
#include<stdio.h>
#include<conio.h>
void swap(int *x,int *y);
void main()
{
int a,b;
clrscr();
a=10;
b=20;
printf("Before swapping a =%d b=%d\n",a,b);
swap(&a,&b);
Output:-
getch();
Before swapping a =10 b=20
}
After swapping a =20 b=10
void swap(int *x,int *y)
{
*x=*x+*y;
*y=*x-*y;
*x=*x-*y;
printf("After swapping a =%d b=%d",*x,*y);
}
5) Address arithmetic
The following are the address arithmetic operations.
1) Incrementing a pointer
2) Decrementing a pointer
3) Adding an integer to pointer and
4) Subtracting an integer to pointer.
&x[i] (x+i)
1D array
x[i] *(x+i)
&x[i][j] *(x+i)+j
2D array
x[i][j] *(*(x+i)+j)
Consider the following example for storing values into an array and displaying array
values.
For 1D array is, int A[5],i; and For 2D array is, int A[5][3],i,j;
for(i=0;i<m;i++) for(i=0;i<m;i++)
{ {
for(j=0;j<q;j++) for(j=0;j<q;j++)
{ {
C[i][j]=0; *( *(C+i)+j )=0;
for(k=0;k<n;k++) for(k=0;k<n;k++)
{ {
} *( *(A+i)+k ) * *( *(B+k)+j );
} }
} }
}
printf("Multiplication of two matrices:\n"); printf("Multiplication of two matrices:\n");
for(i=0;i<m;i++) for(i=0;i<m;i++)
{ {
for(j=0;j<q;j++) for(j=0;j<q;j++)
{ {
printf("%d",C[i][j]); printf("%d",*( *(C+i)+j ));
} }
} }
}/*end of the if statement*/ }/*end of the if statement*/
else else
{ {
printf("Matrix Multiplication is not printf("Matrix Multiplication is not
possible\n"); possible:\n");
} }
getch(); getch();
}/*end of the main() function*/ }/*end of the main() function*/
Output: Output:
Run 1: Run 1:
rows and columns of A Matrix: rows and columns of A Matrix:
3 3
2 2
rows and columns of B Matrix: rows and columns of B Matrix:
2 2
2 2
Matrix Multiplication is possible: Matrix Multiplication is possible:
Enter elements of matrix A: Enter elements of matrix A:
123456 123456
Enter elements of matrix B: Enter elements of matrix B:
10 20 30 40 10 20 30 40
Multiplication of two matrices: Multiplication of two matrices:
Run 2: Run 2:
rows and columns of A Matrix: rows and columns of A Matrix:
2 2
2 2
rows and columns of B Matrix: rows and columns of B Matrix:
3 3
2 2
Matrix Multiplication is not possible Matrix Multiplication is not possible
7)
Prepared by M.SATYANARAYANA REDDY, Asst.Prof. in CSE Dept.,VIKAS ENGG.COLLEGE.
10
8)
7) Dynamic memory management functions
The memory which is allocated at the time of compilation is called static memory allocation.
In this case there is no chance to reduce the memory size for storing the lesser values and no
chance to increase the memory size to store the more no. Of values to actual size. So, in static
memory allocation there is a chance to occur two problems.
i) Memory wastage and
ii) Memory shortage.
We will overcome the above two problems using Dynamic Memory Management Functions:
1) malloc( )
2) calloc( )
3) realloc( ) and
4) free( )
1) malloc( ):- It allocates the requested size of bytes and returns the pointer to the first
byte.
Syntax:- int *ptr;
ptr=(data-type*)malloc(byte-size);
2) calloc( ):- It allocates the requested blocks of memory and returns the pointer to the
first block.
Syntax:- int *ptr;
ptr=(data-type*)calloc(n,block-size);
3) realloc( ):- It is used to re-allocate the memory which is dynamically allocated in
earlier.
Syntax:- int *ptr;
ptr=(data-type*)realloc(ptr,new-size);
4) free( ) :-It is used to release a block of memory as,
free(ptr);
The use of character pointer can be explained with the following notations.
Using array notation Using pointer notations
void strcopy(char str1[ ],char str2[ ]) void strcopy(char *str1,char *str2)
{ {
int i; while(*str2!=’\0’)
i=0; { *str1++=*str2++;
while(str2[i]!=’\0’) }
{ str1[i]=str2[i]; *str1=’\0’;
i++; }
}
str1[i]=’\0’;
In the above example the function strcopy(str1,str2), the string from str2 is copied into the
string str1.
In pointer usage, the base address of str2 and the target name of string str1 is supplied to the
strcopy( ) function.
The last cell of the string is filled with ‘\0’ , to indicate the end position of a string.