0% found this document useful (0 votes)
16 views12 pages

Unit Iv

Uploaded by

chandram.ai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views12 pages

Unit Iv

Uploaded by

chandram.ai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1

UNIT IV
POINTERS

Objective: Understanding pointers and dynamic memory allocation POINTERS:


pointers- concepts, initialization of pointer variables, pointers and function arguments,
passing by address- dangling memory, address arithmetic, character pointers and functions,
pointers to pointers, pointers and multi-dimensional arrays, dynamic memory management
functions, command line arguments

SAMPLE QUESTIONS

1) What is pointer? Explain about initialization of pointer variables?


2) Explain about pointers and function arguments(call-by-referrence)?
3) Explain about address arithmetic ?
4) Explain about character pointers and functions?
5) Explain about pointers to pointers?
6) Explain about pointers and multi-dimensional arrays?
7) Explain about dynamic memory management functions?
8) Explain about command line arguments?.

Prepared by M.SATYANARAYANA REDDY, Asst.Prof. in CSE Dept.,VIKAS ENGG.COLLEGE.


2

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.

2) Pointer Operators:-(& and *)


Address Operator(&):-
It returns the address of a variable.
Example:- int a, *x;
x=&a;
Indirection (or) Differencing operator(*):-
It returns the value at that address.
Example:- int a,b,*x;
a=10;
x=&a;
b=*x;
so , finally the value of ‘a’ is 10 as well as ‘b’ is 10.
Example program:-
Write a C program to store the values and display the values into variable using pointers
concept.
#include<stdio.h>
#include<conio.h> Output
void main() a value: 10
{ int a,b,*x; b value: 10
clrscr();
a=10;
x=&a;
b=*x;
printf("a value: %d\n",a);
printf("b value: %d",b);
}

Prepared by M.SATYANARAYANA REDDY, Asst.Prof. in CSE Dept.,VIKAS ENGG.COLLEGE.


3

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);
}

Prepared by M.SATYANARAYANA REDDY, Asst.Prof. in CSE Dept.,VIKAS ENGG.COLLEGE.


4

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.

Pointer variable Pointer value Arithmetic Result


operations
char *x; 20 x++ 21(x+1*sizeof(char))
or
x -- 19(x-1*sizeof(char))
or
x=x+3 23(x+3*sizeof(char))
or
x=x-3 17(x-3*sizeof(char))

int *x; 20 x++ 22(x+1*sizeof(int))


or
x -- 18(x-1*sizeof(int))
or
x=x+3 26(x+3*sizeof(int))
or
x=x-3 14(x-3*sizeof(int))

float *x; 20 x++ 24(x+1*sizeof(float))


or
x -- 16(x-1*sizeof(float))
or
x=x+3 32(x+3*sizeof(float))
or
x=x-3 8(x-3*sizeof(float))

double *x; 30 x++ 38(x+1*sizeof(double))


or
x -- 22(x-1*sizeof(double))
or
x=x+3 54(x+3*sizeof(double))
or
x=x-3 6(x-3*sizeof(double))

Prepared by M.SATYANARAYANA REDDY, Asst.Prof. in CSE Dept.,VIKAS ENGG.COLLEGE.


5

6) Pointers and Arrays


The pointers can be implemented in 1D and 2D arrays as follows:

Array Without pointers With pointers

&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;

Array Without pointers With pointers

Storing for(i=0;i<5;i++) for(i=0;i<5;i++)


{ {
scanf(“%d”,&A[i]); scanf(“%d”,(A+i));
} }
1D array displaying for(i=0;i<5;i++) for(i=0;i<5;i++)
{ {
printf(“%d”,A[i]); printf(“%d”,*(A+i));
} }
Storing for(i=0;i<5;i++) for(i=0;i<5;i++)
{ {
for(j=0;j<3;i++) for(j=0;j<3;i++)
{ {
scanf(“%d”,&A[i][j]); scanf(“%d”, (*(A+i)+j));
} }
2D array } }
displaying for(i=0;i<5;i++) for(i=0;i<5;i++)
{ {
for(j=0;j<3;i++) for(j=0;j<3;i++)
{ {
printf (“%d”,&A[i][j]); printf (“%d”, *(*(A+i)+j));
} }
} }

Write a C program to implement the matrices addition using pointers.

Prepared by M.SATYANARAYANA REDDY, Asst.Prof. in CSE Dept.,VIKAS ENGG.COLLEGE.


6

Without pointers Using pointers


#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
int A[10][10],B[10][10],C[10] int A[10][10],B[10][10],C[10]
[10],i,j,m,n,p,q; [10],i,j,m,n,p,q;
clrscr(); clrscr();
printf(" rows and columns of A printf(" rows and columns of A
Matrix:\n"); Matrix:\n");
scanf("%d%d",&m,&n); scanf("%d%d",&m,&n);
printf(" rows and columns of B Matrix:\ printf(" rows and columns of B Matrix:\
n"); n");
scanf("%d%d",&p,&q); scanf("%d%d",&p,&q);
if(m==p && n==q) if(m==p && n==q)
{ printf("Matrix Addition is possible:\ { printf("Matrix Addition is possible:\
n"); n");
printf("Enter elements of matrix A:\n"); printf("Enter elements of matrix A:\n");
for(i=0;i<m;i++) for(i=0;i<m;i++)
{ {
for(j=0;j<n;j++) for(j=0;j<n;j++)
{ {
scanf("%d" ,&A[i][j]); scanf("%d" , *(A+i)+j);
} }
} }
printf("Enter elements of matrixB:\n"); printf("Enter elements of matrix B:\n");
for(i=0;i<m;i++) for(i=0;i<m;i++)
{ {
for(j=0;j<n;j++) for(j=0;j<n;j++)
{ {
scanf("%d",&B[i][j]); scanf("%d",*(B+i)+j);
} }
} }
for(i=0;i<m;i++) for(i=0;i<m;i++)
{ {
for(j=0;j<n;j++) for(j=0;j<n;j++)
{ { *( *(C+i)+j )= *( *(A+i)+j )+
C[i][j]= A[i][j]+B[i][j]; *( *(B+i)+j );
} }
} }

Prepared by M.SATYANARAYANA REDDY, Asst.Prof. in CSE Dept.,VIKAS ENGG.COLLEGE.


7

printf("Sum of two matrices:\n"); printf("Sum of two matrices:\n");


for(i=0;i<m;i++) for(i=0;i<m;i++)
{ {
for(j=0;j<n;j++) for(j=0;j<n;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 Addition is not possible\ printf("Matrix Addition is not possible\
n"); n");
} }
getch(); getch();
} }
Output: Output:
Run 1: Run 1:
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:
2 2
2 2
Matrix Addition is possible: Matrix Addition is possible:
Enter elements of matrix A: Enter elements of matrix A:
1234 1234
Enter elements of matrix B: Enter elements of matrix B:
10 20 30 40 10 20 30 40
Sum of two matrices: Sum of two matrices:
11 22 11 22
33 44 33 44
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 Addition is not possible Matrix Addition is not possible

Prepared by M.SATYANARAYANA REDDY, Asst.Prof. in CSE Dept.,VIKAS ENGG.COLLEGE.


8

Write a C program to implement the matrices multiplication using pointers.


Without pointers Using pointers
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
int A[10][10],B[10][10],C[10][10],i,j, int A[10][10],B[10][10],C[10][10],i,j,
m,n,p,q,k; m,n,p,q,k;
clrscr(); clrscr();
printf(" rows and columns of A Matrix:"); printf(" rows and columns of A Matrix:");
scanf("%d%d",&m,&n); scanf("%d%d",&m,&n);
printf(" rows and columns of B Matrix:"); printf(" rows and columns of B Matrix:");
scanf("%d%d",&p,&q); scanf("%d%d",&p,&q);
if(n==p) if(n==p)
{ {
printf("Matrix Multiplication is possible:\ printf("Matrix Multiplication is
n"); possible:\n");
printf("Enter elements of matrix A:\n"); printf("Enter elements of matrix A:\n");
for(i=0;i<m;i++) for(i=0;i<m;i++)
{ {
for(j=0;j<n;j++) for(j=0;j<n;j++)
{ {
scanf("%d" ,&A[i][j]); scanf("%d" , *(A+i)+j);
} }
} }
printf("Enter elements of matrix B:\n"); printf("Enter elements of matrix B:\n");
for(i=0;i<p;i++) for(i=0;i<p;i++)
{ {
for(j=0;j<q;j++) for(j=0;j<q;j++)
{ {
scanf("%d",&B[i][j]); scanf("%d",*(B+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++)
{ {

C[i][j]= C[i][j]+A[i][k]*B[k][j]; *( *(C+i)+j )= *( *(C+i)+j )+

Prepared by M.SATYANARAYANA REDDY, Asst.Prof. in CSE Dept.,VIKAS ENGG.COLLEGE.


9

} *( *(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);

Writer a C program to explain the concept of Dynamic Memory Management


functions.
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void main()
{ Output:
int *ptr,n; enter n value: 7
clrscr(); name is: Guntur
printf("enter n value :\n"); enter n value: 11
scanf("%d",&n); name is: vijayawada
ptr=(char*)malloc(n*sizeof(char));
strcpy(ptr,"guntur");
printf("name is:%s\n",ptr);
printf("enter n value:\n");
scanf("%d",&n);
ptr=(char*)realloc(ptr,n*sizeof(char));
strcpy(ptr,"vijayawada");
printf("name is:%s\n",ptr);
free(ptr);
getch();

Prepared by M.SATYANARAYANA REDDY, Asst.Prof. in CSE Dept.,VIKAS ENGG.COLLEGE.


11

8) Command line arguments


i) Command line arguments are actual arguments/parameters. These arguments
are provided at the place of command prompt.
ii) These command line arguments are received by the formal parameters of
main( ) function.
iii) The main( ) function consists of argc, argv[ ] as formal parameters. where ,
‘argc’ means the no.of parameters passed from command prompt to
main( ) function of the program.
‘argv’ means argv[0] is always represents the executable program.
argv[1], argv[2],...are the parameters passed to the
main( )function.
Example:- To copy the contents of A.txt to B.txt. we will use the following command
C:\>COPY A.txt B.txt
In this example the no. Of arguments , argc=3
Executable program name is COPY
Source file is A.txt
Destination file is B.txt
Write a C program to explain the concept of command line arguments.
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main(int argc,char *argv[])
{
FILE *sfptr,*dfptr;
char ch; Output
clrscr(); The program is executed as follows:
if(argc!=3) C:\> COPY A.txt B.txt
{ As a result the contents of A.txt is copied to B.txt
printf(“Invalid number of parameters”);
exit(1);
}
sfptr=fopen(argv[1],”r”);
dfptr=fopen(argv[1],”w”);
while(!feof(sfptr))
{
ch=fgetc(sfptr);
fputc(ch,dfptr);
}
fcloseall();
getch();
}

Prepared by M.SATYANARAYANA REDDY, Asst.Prof. in CSE Dept.,VIKAS ENGG.COLLEGE.


12

9) Character pointers and functions

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.

Write a C program to explain the concept of character pointers and functions.


#include<stdio.h>
#include<conio.h>
void strcopy(char *str2,char *str1);
void main()
{ Output
char str1[10]="satyam",str2[10]; string1 data: satyam
clrscr(); string2 data: satyam
strcopy(str2,str1);
printf("string1 data:%s\n",str1);
printf("string2 data:%s",str2);
getch();
}
void strcopy(char *str2,char *str1)
{
while(*str1!='\0')
{
*str2++=*str1++;
}
*str2='\0';
}

Prepared by M.SATYANARAYANA REDDY, Asst.Prof. in CSE Dept.,VIKAS ENGG.COLLEGE.

You might also like