UNIT-4
UNIT-4
POINTERS
INTRODUCTION:
POINTER:
Syntax:
datatype *pointervariable;
Ex:
int *i;//integer pointer variable ‘i’ which stores address of integer variable.
float *b;// floating pointer variable ‘b’ which stores address of floating point variable.
char *ch;// character pointer variable ‘ch’ which stores address of character variable.
Syntax:
datatype *pointername;
pointername=&variablename;
Ex:
int *a,b=10; a b
2002 10
a=&b;
2000 2002
#include<stdio.h>
#include<conio.h>
main()
{ a ptr
printf(“value of a is %d\n”,a);
printf(“value of a is %d\n”,*ptr);
printf(“value of a is %d\n”,*(&a));
printf(“value of a is %d\n”,*(*(&ptr)));
printf(“address of a is %u\n”,ptr);
printf(“address of a is %u\n”,&a);
getch();
Output:
value of a is 10
value of a is 10
value of a is 10
value of a is 10
address of a is 1000
address of a is 1000
Types of pointers:
1. NULL POINTER:
• A null pointer is a special pointer that doesn’t point to any value. This means that a
null pointer doesn’t point to any valid memory address.
• To declare a null pointer, you may use the predefined constant NULL,which is defined
in several standard header files including <stdio.h>,<stdlib.h> and <string.h>
Ex: int *ptr=NULL;
• We can check whether a given pointer variable stores address of some variable or
contains a NULL string by writing
If(ptr==NULL)
{
Statement block;
}
• We may also initialize a pointer as a NULL pointer by using a constant 0,as shown
below:
int ptr;
ptr=0;
• Null pointers are used in situations where one of the pointers in the program points to
different locations at different times.
• In such situations ,it is always better to set it to a NULL pointer when it doesn’t point
anywhere valid, and to test to see if it’s a null pointer before using it.
2. GENERIC POINTER:
Example program:
#include<stdio.h>
#include<conio.h>
main()
int x=10;
char ch=’a’;
float f=1.23;
void *gp;
gp=&x;
printf(“generic pointer points to the integer value=%d”,*(int *)gp);
gp=&ch;
gp=&f;
getch();
Output:
POINTER TO POINTER:
• In this, by using one pointer, we will store the address of another pointer i.e.,
pointer stores the address of another pointer.
Syntax:
datatype **pointervariable;
#include<stdio.h>
#include<conio.h>
main()
ptr1=&a;
ptr2=&ptr1;
ptr3=&ptr2;
printf(“the value of a is %d\n”,a);
getch();
Output:
the value of a is 10
COMPATABILITY:
• You can assign an integer value to a double variable without using type conversion
in numeric types, but we cannot do the same to the pointers.
Example program:
#include<stdio.h>
#include<conio.h>
main()
{
int a=10;
double d=20.1234567;
int *ptr1,*ptr2;
ptr1=&a;
ptr2=&d;//error because ptr2 is integer pointer and ‘d’ is double value
printf(“the value of ptr1 is %u\n”,ptr1);
printf(“the value of ptr2 is %u\n”,ptr2);
getch();
}
• “l-value” refers to memory location which identifies an object. l-value may appear as
either left hand or right hand side of an assignment operator(=).
• l-value often represents as identifier.
• The name of the identifier denotes a storage location.
Operators that require L-value expression:
TYPE OF EXPRESSION EXAMPLES
Address operator &score
Postfix increment/decrement X++,y--
Prefix increment/decrement ++x,--y
Assignment(left operand) X=1,y+=3(y=y+3)
EXPRESSION PROBLEM
A+2=6; A+2 is an r-value and cannot be the left
operand in an assignment. It is a
temporary value that doesn’t have an
address;no place to store 6
&(a+2) (a+2) is an r-valu,and the address
operator needs l-value.
&4; Same as above(4 is an r-value).
II POINTER APPLICATIONS
POINTER TO ARRAYS:
• In pointer to array, the pointer stores address of the array. suppose we declare an
array x as follows:
int x[5]={6,0,2,5,1};
Then the elements will be stored as
Value→ 6 0 2 5 1
• Now if an integer pointer *ptr is declared and the address of 1st element is stored in
ptr,then
int *ptr;
ptr=&x[0];
Then,
ptr=&x[0]=2000;
ptr+1=&x[1]=2002;
ptr+2=&x[2]=2004;
ptr+3=&x[3]=2006;
ptr+4=&x[4]=2008;
so,
*(ptr+2)=x[2]=2;
#include<stdio.h>
#include<conio.h>
main()
int x[5]={6,0,2,5,1};
int *ptr,i;
for(i=0;i<5;i++)
ptr++;
getch();
Output:
Row1 Row2
Values-→ 2 1 3 4
Index→
a[0][0] a[0][1] a[1][0] a[1][1]
Memory location 2000 2002 2004 2006
• Now if the base address of the array i.e., &a[0][0] is assigned to pointer ‘ptr’,then the 2D
array can be traversed by incrementing ‘ptr’ i.e.,
int *ptr;
ptr=&a[0][0];
ptr+1=&a[0][1];
ptr+2=&a[1][0];
ptr+3=&a[1][1];
Then *(&(a[0][1]))=1
Program to explain the concept of pointers and 2D arrays:
#include<stdio.h>
#include<conio.h>
main()
{
int a[2][2]={{2,1},{3,4}};
int *ptr,i,j;
printf(“elements of 2d array are:\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,*ptr);
ptr++;
}
printf(“\n”);
}
}
POINTER ARITHMETIC:
The arithmetic operations that we can perform on pointers are:
1. Incrementation of pointers
2. Decrementation of pointers
3. Adding a number to pointer
4. Subtracting a number from pointer
5. Subtracting two pointer variables
#include<stdio.h>
#include<conio.h>
main()
int *ptr1,*ptr2;
int a[5]={10,20,30,40,50};
ptr1=&a[0];
ptr1++;
ptr2=&a[4];
ptr2--;
ptr1=ptr1+2;
ptr2=ptr2-2;
getch();
ARRAY OF POINTERS:
Syntax:
datatype *arrayname[size];
Ex: int *a[10];//by using this, we can store 10 addresses of integer values.
float *c[5];//by using this, we can store 5 addresses of floating point values.
#include<stdio.h>
#include<conio.h>
main()
int a=10,b=20,c=30; a 10 b 20 c 30
clrscr(); ptr
ptr[0]=&a;
ptr[1]=&b; ptr[0] ptr[1] ptr[2] ptr[3] ptr[4] ptr[5] ptr[6] ptr[7] ptr[8] ptr[9]
ptr[2]=&c; 4000 4002 4004 4006 4008 4010 4012 4014 4016 4018
getch();
MEMORY ALLOCATION:
2.once memory is allocated, it is not possible to change the size of the array.
Ex: int a[100];if we the memory to store 5 elements then remaining memory is wasted.
4.if less memory is allocated than the required, we can’t store more elements.
Memory allocation(malloc):
• Malloc is mainly used to allocate memory to a pointer variable during execution time.
• It allocates requested size in terms of bytes and returns pointer to the starting address
of the memory block.
• Malloc allocates only one block of memory.
Syntax:
datatype *pointervariable=(datatype *)malloc(type size);
Ex: int *ptr=(int *)malloc(n*sizeof(2));
If the number of elements are 5 then 10 bytes of memory is allocated for pointer
variable ptr.
• Malloc function accepts only one argument i.e., byte size.
• The default initial values are garbage values.
#include<stdio.h>
#include<string.h>
main()
{
int i,n;
int *ptr;
scanf("%d",&n);
ptr=(int *)malloc(n*sizeof(int));
for(i=0;i<n;i++)
scanf("%d",&ptr[i]);
for(i=0;i<n;i++)
printf("%d\t",ptr[i]);
Output:
10 20 30 40 50
10 20 30 40 50
#include<stdio.h>
#include<stdlib.h>
main()
struct student
int rno;
float per;
};
if(ptr==NULL)
else
scanf("%d",&ptr->rno);
scanf("%f",&ptr->per);
Output:
98
percentage of student is 98
CONTIGUOUS ALLOCATION(CALLOC):
#include<stdio.h>
main()
int i,n;
int *ptr;
scanf("%d",&n);
ptr=(int *)calloc(n,sizeof(int));
for(i=0;i<n;i++)
scanf("%d",&ptr[i]);
}
for(i=0;i<n;i++)
printf("%d\t",ptr[i]);
Output:
10 20 30 40 50
10 20 30 40 50
REALLOC FUNCTION:
realloc(pointervariable,bytesize);
#include<stdio.h>
#include<stdlib.h>
main()
char *str;
str=(char *)malloc(5);
if(str==NULL)
strcpy(str,"hell");
str=(char *)realloc(str,20);
if(str==NULL)
Output:
Free:
• Free function frees the memory allocated by malloc and calloc functions.