You are on page 1of 44

6.

1 Pointer :Introduction, Definition and uses


of Pointers, Address Operator, Pointer
Variables, Dereferencing Pointer, Void Pointer,
Pointer Arithmetic, Pointers to Pointers,
Pointers and Array, Passing Arrays to Function,
Pointers and two dimensional Array, Array of
Pointers, Dynamic Memory Allocation.
 A pointer is a variable
whose value is the address
of another variable.

 Pointer stores the address


of memory and not the
value.

 Like any variable or


constant, you must declare
a pointer before using it to
store any variable address.
 When a pointer variable is declared, the variable
name must be preceded by an asterisk (*).

 This * symbol is used to indicate that the variable


is a pointer variable.

 The data type that appears in the declaration of


pointer will be the data type of the variable to
which it points.

 Syntax :
data_type *ptvar;
Where, ptvar is name of pointer variable
data_type is the data type of the variable
e.g. float a;
float *pa;
 Pointer initialization and accessing pointer
variable

 Syntax of pointer initialization:


pointer_variable=&variable;

e.g. int *pk;


pk=&k; //Pointer initialization
int b=*pk; //accessing pointer variable
Reference operator (&)
 Referencing means taking the address of an existing
variable (using &) to set a pointer variable.
 In order to be valid, a pointer has to be set to the
address of a variable of the same type as the pointer,
without the asterisk

Dereference operator (*)


 Dereferencing a pointer means using the * (asterisk)
operator to access the value stored at a pointer:
 The value stored at the address of the pointer must be a
value OF THE SAME TYPE as the type of variable the
pointer "points“ to.
#include <stdio.h>
#include<conio.h>
void main ()
{
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
clrscr();
ip = &var; /* store address of var in pointer variable*/
printf("value of var using its name: %d\n",var);
printf("Address of var using its name: %x\n", &var);
/* address stored in pointer variable */
printf("Address of var stored in ip: %x\n", ip);
/* access the value using the pointer */
printf("Value of var using *ip: %d\n", *ip);
getch();
}
 WAP to add two numbers using pointers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
int b=20;
int sum;
int *pa,*pb;
pa=&a;
pb=&b;
sum=*pa+*pb;
printf("sum=%d",sum);
getch();
}
 Void pointer or generic pointer is a special type
of pointer that can be pointed at objects of any
data type.
 A void pointer is declared like a normal pointer,
using the void keyword as the pointer’s type.
 Pointers defined using specific data type cannot
hold the address of the some other type of
variable i.e., it is incorrect to assign the address
of an integer variable to a pointer of type float.
 Syntax:
void *v // defines a pointer of type void
 The pointer defined in this manner do not have
any type associated with them and can hold the
address of any type of variable.
void *v;
int *i;
int ivar;
char chvar;
float fvar;
v = &ivar; // valid
v = &chvar; //valid
v = &fvar; // valid
i = &ivar; //valid
i = &chvar; //invalid
i = &fvar; //invalid
 A pointer in c is an address, which is a
numeric value.
 Therefore, you can perform arithmetic
operations on a pointer just as you can on a
numeric value.
 There are four arithmetic operators that can
be used on pointers: ++, --, +, and -
#include<stdio.h>
#include<conio.h>
void main()
{
int x=3;
int *px;
float k=3.14;
float *pk;
char j='d';
char *pj;
px=&x;
pk=&k;
pj=&j;
printf("\nthe value of x=%d and address is %u",x,px);
printf("\nthe value of k=%f and address is %u",k,pk);
printf("\nthe value of j=%c and address is %u",j,pj);
printf(“\n %d %f %c”, *px,*pk,*pj);
contd…
++px;
++pk;
++pj;
printf("\n\nthe value of x=%d and address is %u",x,px);
printf("\nthe value of k=%f and address is %u",k,pk);
printf("\nthe value of j=%c and address is %u",j,pj);
printf(“\n %d %f %c”, *px,*pk,*pj);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int x=3;
int *px;
float k=3.14;
float *pk;
char j='d';
char *pj;
clrscr();
px=&x;
pk=&k;
pj=&j;
printf("\nthe value of x=%d and address is %u",x,px);
printf("\nthe value of k=%f and address is %u",k,pk);
printf("\nthe value of j=%c and address is %u",j,pj);
printf(“\n %d %f %c”, *px,*pk,*pj);
contd…
--px;
--pk;
--pj;
printf("\n\nthe value of x=%d and address is
%u",x,px);
printf("\nthe value of k=%f and address is
%u",k,pk);
printf("\nthe value of j=%c and address is %u",j,pj);
printf(“\n %d %f %c”, *px,*pk,*pj);
getch();
}
#include <stdio.h>
const int MAX = 3;
int main ()
{
int var[] = {10, 100, 200};
int i, *ptr;
/* let us have address of the first element in pointer */
ptr = var;
i = 0;
while ( ptr <= &var[MAX - 1] )
{
printf("Address of var[%d] = %x\n", i, ptr );
printf("Value of var[%d] = %d\n", i, *ptr );
ptr++;
i++;
}
return 0;
}
 Addition of two pointers

 Multiplying a pointer with a number

 Dividing a pointer with a number

Note : In the slide no.10 we have done addition of


the values using pointer variable, not the addition
of pointers.
 An array name is a constant pointer to the
first element of the array.

 A pointer when incremented, always points to


a location after skipping the number of bytes
required for the data type pointed to by it.
#include<stdio.h>
#include<conio.h>
void main()
{
int num[]={24, 34, 12, 44, 56, 17};
int i,*j;
clrscr();
j=&num[0];
for(i=0;i<=5;i++)
{
printf("\n address=%d",j);
printf("\t element=%d",*j);
j++;
}
getch();
}
 Write a program to find sum of all element
present in an array using pointer.
#include<stdio.h>
#include<conio.h>
void main() {
int numArray[10];
int i, sum = 0;
int *ptr;
clrscr();
printf("\nEnter 10 elements : ");
for (i = 0; i < 10; i++)
scanf("%d", &numArray[i]);
ptr = numArray; /* ptr=&numArray[0]; */
for (i = 0; i < 10; i++)
{
sum = sum + *ptr;
ptr++;
}
printf("The sum of array elements : %d", sum);
getch();
}
 Write a program to find largest element of an
array using pointer.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i;
int n,max;
int *p;
clrscr();
printf("\n Enter number");
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
p=a;
max=*p;
for(i=0;i<n;i++)
{ if(*p>max)
{ max=*p;
}
p++;
}
printf("largest=%d",max);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int
num[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int i,*j;
clrscr();
j=&num[0][0];
for(i=0;i<=9;i++)
{
printf("\n address=%d",j);
printf("\t element=%d",*j);
j++;
}
getch();
}
 When the data items are of pointer type, it is
known as a pointer array or an array of pointers.

 Since a pointer variable always contains an


address, an array of pointer is collection of
addresses.

 The address present in the array of pointers can


be address of different variables or address of
array elements or any other addresses.

 E.g. int *sum[5];


#include <stdio.h>
#include<conio.h>
int main ()
{
int var[] = {10, 100, 200};
int i, *ptr[3];
clrscr();
for ( i = 0; i < 3; i++)
{
ptr[i] = &var[i]; /* assign the address of integer. */
}
for ( i = 0; i < 3; i++)
{
printf("Value of var[%d] = %d\n", i, *ptr[i] );
printf(“&ptr=%d &var=%d\n",&ptr[i],&var[i]);
}
getch();
return 0;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int x=25,y=50,z=60;
int i,*arr[3];
arr[0]=&x;
arr[1]=&y;
arr[2]=&z;
for(i=0;i<3;i++)
{
printf("value is: %d\t",*arr[i]);
printf("&arr=%u\n",&arr[i]);
}
printf("\n&x=%u\n",&x);
printf("&y=%u\n",&y);
printf("&z=%u\n",&z);
getch();
}
 A pointer to a pointer is a form of multiple
indirection, or a chain of pointers.

 When we define a pointer to a pointer, the first


pointer contains the address of the second pointer,
which points to the location that contains the actual
value.

 E.g. int **ptr;


#include <stdio.h>
#include<conio.h>
int main ()
{
int var;
int *ptr;
int **pptr;
clrscr();
var = 3000;
ptr = &var; //take the address of var
pptr = &ptr; // take the address of ptr using &
printf("Value of var = %d\n", var );
printf("Value available at *ptr = %d\n", *ptr );
printf("Value available at **pptr = %d\n", **pptr);
getch();
return 0;
}
 The concept of dynamic memory allocation in
c language enables the C programmer to
allocate memory at runtime.

 Dynamic memory allocation in c language is


possible by 4 functions of stdlib.h header file.

◦ malloc()
◦ calloc()
◦ realloc()
◦ free()
 The malloc() function allocates single block of
requested memory.

 It doesn't initialize memory at execution time,


so it has garbage value initially.

 It returns NULL if memory is not sufficient.

 Syntax :
ptr=(cast-type*)malloc(byte-size)
#include <stdio.h>
#include <stdlib.h>
void main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
//memory allocated using malloc
ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");

exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
}
 The calloc() function allocates multiple block
of requested memory.

 It initially initialize all bytes to zero.

 It returns NULL if memory is not sufficient.

Syntax :
ptr=(cast-type*)calloc(number, byte-size)
include <stdio.h>
#include <stdlib.h>
void main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
//memory allocated using calloc
ptr=(int*)calloc(n,sizeof(int));
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");

exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
}
 If memory is not sufficient for malloc() or
calloc(), you can reallocate the memory by
realloc() function.

 In short, it changes the memory size.

Syntax :
ptr=realloc(ptr, new-size)
 The memory occupied by malloc() or calloc()
functions must be released by calling free()
function.

 Otherwise, it will consume memory until


program exit.

 Syntax:
free(ptr);

You might also like