UNIT V
Pointers
Contents
Pointers:
Introduction to Pointers
Declaring pointer variables
Programs using pointers
Introduction to Pointers
• Some C programming tasks can be performed more
easily with pointers.
• Also, there are some tasks such as dynamic memory
allocation which can not be performed without using
pointers.
• In simple words, a pointer is an address.
• A pointer is used to hold or store the address of a
variable of any data type.
• It is a derived data type that stores a memory address.
Derived Data Types
Function Array Pointer Structure Union Enumerated
Type Type Type Type Type Type
• Every variable in C has a name and a value associated
with it.
• When a variable is declared, a specific block of memory
is allocated to hold the value of that variable.
• The size of the allocated block depends on the type of
the data.
• A pointer provides access to a variable by using address
of that variable.
int i=3 ;
i Location name
3 Value at location
65542 Location number or Address
Every variable in C has a value and a memory
location (address) associated with it.
//Printing a pointer
#include<stdio.h>
int main()
{
int i=3;
printf("Address of i= %u\n", &i);
printf("Value of i= %d", i);
return 0;
}
Output:
Address of i= 6356748
Value of i= 3
• A pointer is a constant or variable that contains an
address that can be used to access data.
• Pointers have many uses in C.
They provide a very efficient method of accessing data.
They provide an efficient technique for manipulating data in
arrays.
They can be used in functions as pass-by-address parameters.
They are the basis for dynamic allocations of memory.
Some more facts about pointers
• We can have pointer variables that point to all types of
variables – int, float, char, double.
• A pointer can also be used to point to another pointer
variable.
• A pointer can be used to point to a function
(Function pointers) .
• Pointers provide a handy way to access arrays and
strings.
• Pointers enable a programmer to return multiple data
items from a function.
Declaring a Pointer
• Like variables, pointers have to be declared before they
are used.
• Pointer is a variable that stores the address of another
variable.
• A pointer variable is declared as follows.
data_type *ptr_name;
For ex. int *pnum; // pnum is a pointer that
points to an integer data
char *pchar;
float *pfnum;
//pointer variable
#include<stdio.h> Output:
int main()
{ 1.Address of i= 6356748
int i=3;
int *j; // j points to an integer 2.Address of i= 6356748
j=&i ; // & is address of operator
3.Address of j= 6356744
printf("\n1.Address of i= %u\n", &i);
printf("\n2.Address of i= %u\n\n", j);
printf("\n3.Address of j= %u\n\n",&j); 4.Value of i= 3
printf("\n4.Value of i= %d\n", i);
printf("\n5.Value of i= %d\n",*j); 5.Value of i= 3
//* stands for 'value at address
printf("\n6.Value of i= %d",*(&i)); 6.Value of i= 3
return 0;
}
// to calculate area of a circle using pointers
#include<stdio.h>
int main()
{
float rad,area;
Output:
float *ptrrad, *ptrarea;
ptrrad=&rad;
Enter the radius of the circle :2.0
ptrarea=&area;
printf("\n Enter the radius of the circle :");
The area of the circle is 12.56
scanf("%f", ptrrad);
*ptrarea= 3.14 * (*ptrrad) * (*ptrrad);
printf("\n\n The area of the circle is %.2f",
*ptrarea);
return 0;
}
Programs for practice
• Write a C program to add two integer numbers using
pointers.
• Write a C program to add two floating point numbers
using pointers.
• Write a C program to find sum and average of three
numbers. Access all the variables using pointers.
• Write a C program to find the largest of three numbers
using pointers.