BCA Paper-V Unit-8
BCA Paper-V Unit-8
BCA Paper-V Unit-8
Lesson Structure
8.0 Objective
8.1 Introduction
8..2 Pointer and its Characteristics
8.3 Address and Indirection Operators
8.4 Pointer Type Declaration and Assignment
8.5 Pointer Arithmetic
8.6 Passing Pointer to Functions
8.7 Pointer to Pointer
8.8 Arrays and Pointer
8.9 Pointer and String
8.10 A function Returning More than One Value
8.11 Function Returning a pointer
8.12 Dynamic Memory Allocation
8.13 Summary
8.14 Questions for exercise
8.15 Suggested Readings
8.0 Objective
8.1 Introduction
A pointer is a derived data type in C. It is built in from one of the fundamental data
types available in C. Pointers contains memory address as their values. Since these memory
addresses are locations in the computer memory where program instructions and data are
stored, pointers can be used to access and manipulate data stored in the memory.
Pointers are undoubtedly one of the most distinct and exiting features of C language.
It has added power and flexibility to the language. Although they appear little confusing and
difficult to understand, but they are very powerful tool for programmers. Pointers are more
efficient in data handling arrays and data tables. Pointers can be used to return multiple
values from a function via function arguments. Pointers allow C to support dynamic memory
management. They reduce length and complexity of programs and increase the execution
speed and reduce the program execution time.
A normal variable is a location in memory that can hold a value. For example, when
you declare a variable i as an integer, two bytes of memory is set aside for it. In your program,
you refer to that location in memory by the name i. At the machine level, location has a memory
address, at which the two bytes can hold one integer value. A pointer is a variable that points
to another variable. This means that it holds the memory address of another variable. Put
another way, the pointer does not hold a value in the traditional sense; instead, it holds the
address of another variable. It points to that other variable by holding its address.
Because a pointer holds an address rather than a value, it has two parts. The pointer
itself holds the address. That addresses points to a value. There is the pointer and the value
pointed to. As long as you’re careful to ensure that the pointers in your programs always point
to valid memory locations, pointers can be useful, powerful, and relatively trouble-free tools.
The computer’s memory is a sequential collection of ‘storage cell’. Each unit is known
as byte, has a number called address associated with it. The addresses are numbered
consecutively, starting from zero. The last address depends on memory size.
198
Pointers
120 value
1000 address
We can acecess the value 120 by using either variable name p or by address 1000.
Since memory addresses are simply numbers they can assigned to other variables, which
can be stored in memory, like any other variable. Such variables which holds memory address
is known as pointer variables. A pointer variable is a variable, that contains an address ,
which is a location of another variable in memory
Variable Value Address
P 120 1000
ptr 1000 1006
Figure 7
Suppose we assign the address of variable p to the pointer variable ptr, now we can
access the value of p by ptr. Since ptr points to the variable p.
Characteristic features of Pointers:
i. The program execution time will be faster as the data is manipulated with the help of
addresses directly.
199
Pointers
The actual location of a variable in the memory is system dependent and therefore,
the address of a variable is not known immediately. The address of a variable in C is determined
by using & operator. The & operator immediately preceding a variable returns the address of
variable associated with it. e.g :
p = #
will assign the address of variable num to the variable p.
C also uses a unary pointer operator “ * “, called value at address or indirection operator. It
returns a value stored at that address.
Example1: write a program to print the address associated with a variable and value stored
at that address.
# include <stdio.h>
# include <conio.h>
void main( )
{
char a;
int x;
float p, q;
a= ‘A’;
x=125;
p= 10.25;
q= 18.75;
printf(“ %c is stored at address %u\n’, &a);
printf(“ %d is stored at address %u\n’, &x);
printf(“ %f is stored at address %u\n’, &p);
printf(“ %f is stored at address %u\n’, &q);
printf(“ the value of a is %c\n’,*( &a));
printf(“ the value of a is %c \n’, a);
200
Pointers
In C, every variable must be declared for its type. Since pointer variables contain
addresses that belong to a separate data type, they must be declared as pointers before we
use them. The general syntax to declare a pointer variable is:
data_type * pointer_var_name;
data type tells that the pointer variable points to a variable of specified data type. The
asterisk(*) tells that the variable is a pointer type variable and the pointer variable name is a
valid identifier.
e.g.
int * p;
declares a pointer variable p as a pointer variable that points to an integer data type.
float *a;
201
Pointers
Like other variables, pointer variable can also be used in expressions. For example if
ptr1 and ptr2 are pointers, then the following statement is valid.
y = (*p1) * (*p2)
sum = sum + (*p1);
z = 7 + (*ptr1/ *ptr2);
*ptr2 = * ptr2 + 10;
In C, the programmer may add or subtract integers from pointers. We can also subtract
one pointer from other. We can also use short hand operators with the pointer variables as
we use with other variables.
202
Pointers
203
Pointers
Pointers provide a mechanism to modify data declared in one function using code
written in another function. when we use pointers to pass arguments to a function we pass
addresses of the variables as arguments and the called function must declare those incoming
204
Pointers
arguments as pointers. To modify the variable sent by the caller, the called function must
dereference the pointers that were passed to it. thus passing pointers to a function avoids the
overhead of copying data from one function to another. The general syntax to declare this
type of function is :
return_ type function_name( datatype 1* , datatype 2*,…, datatype n *);
e.g. int swap(int *, int *);
or int swap(int *a, int *b);
Example 3 : write a program to demonstrate passing pointer to function.
#include<stdio.h>
#include<conio.h>
void sum (int *a, int *b, int *t);
void main()
{
int num1, num2, total;
printf(“\n Enter the first number :”);
scanf(“%d”, &num1);
printf(“\n Enter the second number :”);
scanf(“%d”, &num2);
sum(&num1, &num2, &total);
printf(“\n total = %d”,total);
getch();
}
void sum(int *a, int *b, int *t)
{
*t = *a + *b;
}
O/P:
Enter the first number : 5
Enter the second number : 8
Total = 13
205
Pointers
When we declare a variable it holds the value of the variable, if we declare a pointer it
holds the address of another variable, if we declare a pointer to pointer it will hold the address
of another pointer variable. The general syntax to declare a pointer to pointer is ;
data_type ** pointer_variable_name;
int ** ptr;
this declaration tells the compiler that ptr is a pointer to a pointer of int type. To
retrieve the value of target variable we use the dereference operator(*) twice.
e.g :
int *ptr;
int **ptr1;
int a;
ptr = &a;
ptr1 = &ptr
100 102 104
10 100 102
p p1 p2
Figure 8
Example 4 : write a program to demonstrate pointer to pointer
#include<stdio.h>
#include<conio.h>
void main()
{
int a, *b, **c;
b = &a;
c = &b;
a = 50;
printf(“ %d \t %d \t %d”, a, *b, **c);
** c = 95;
printf(“\n %d\t %d”, *b, a);
206
Pointers
*b = 120;
printf(“ \n %d \t %d\t %d”, a, *b, **c);
getch();
}
O/P:
50 50 50
95 95
120 120 120
When an array is declared, the compiler allocates a base address and sufficient amount
of storage to contain all the elements of the array in contagious memory locations. The base
address is the location of the first element of the array. The compiler also defines the array
name as a constant pointer to the first element. For example if we declare an array as :
int arr[5]= {1,2,3,4,5};
arr[0] arr[1] arr[2] arr[3] arr[4]
1 2 3 4 5
Figure 9
Suppose the base address of arr is 1000 and assuming that each integer requires
two bytes, the five elements will be stored as above.
The name arr is defined as a constant pointer pointing to the first element arr[0] and
therefore the value of arr is 1000, the location where arr[0] is stored.
If we declare p as an integer pointer, then we can make the pointer p to point the array
arr by the following assignment:
P = arr;
This is equivalent to p = &arr[0];
Now, we can access every value of arr using p++ to move from one element to another.
The relationship between p and arr is as follows:
P = &arr[0]
207
Pointers
P +1 = &arr[1]
P +2 = &arr[2]
P +3 = & arr[3]
P +4 = &arr[4]
The address of an element is calculated using its index and the scale factor of the data
type. For instance:
Address of arr[4] = base address + (4* scale factor of int)
= 1000 + (4x 2)
=1008
Example 5 :: write a program to read and display an array of n integers using pointers.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
int arr[10], *ptr;
ptr= arr;
printf( “ enter the number of elements”);
scanf(“%d”, &n);
printf(“\n enter the elements”);
for( i =0 ; i<n ; i++)
scanf(“%d”, ptr+i);
printf(“\n the elements entered are:”);
for( i =0 ; i < n; i++)
printf(“\t %d”, *(ptr +i));
getch();
}
O/P:
Enter the number of elements: 5
Enter the elements : 3 6 8 9 2
The elements entered are : 3 6 8 9 2
208
Pointers
Using call by reference method we can make a function return more than one value at
a time, which is not possible in the call by value method. The following program will makes
you the concept very clear.
Example 7: Write a program to find the perimeter and area of a rectangle, if length and
breadth are given by the user.
#include <stdio.h>
# include <conio.h>
void periarea(float length, float breadth, float *, float *);
void main()
{
float len,br;
float peri, ar;
printf(“\nEnter the length and breadth of a rectangle in metres: \n”);
scanf(“%f %f”,&len,&br);
periarea(len,br,&peri,&ar);
printf(“\nPerimeter of the rectangle is %f metres”, peri);
printf(“\nArea of the rectangle is %f sq. metres”, ar);
}
void periarea(float length, float breadth, float *perimeter, float *area)
{
*perimeter = 2 * (length +breadth);
*area = length * breadth;
}
O/P:
Enter the length and breadth of a rectangle in metres:
23.0 3.0
Perimeter of the rectangle is 52.000000 metres
Area of the rectangle is 69.000000 sq. metres
210
Pointers
Here in the above program, we have seen that the function periarea is returning two
values. We are passing the values of len and br but, addresses of peri and ar. As we are
passing the addresses of peri and ar, any change that we make in values stored at addresses
contained in the variables *perimeter and *area, would make the change effective even in
main() also.
A function can also return a pointer to the calling program, the way it returns an int, a
float or any other data type. To return a pointer, a function must explicitly mention in the calling
program as well as in the function prototype. Let’s illustrate this with an example :
Example 8 : Write a program to illustrate a function returning a pointer.
# include<stdio.h>
# include< conio.h>
float *func( ); /* function prototype */
void main( )
{
float *a;
a = func( );
printf (“Address = %u”, a);
}
float *func( )
{
float r = 5.2;
return (&r);
}
O/P:
Address = 65516
This program only shows how a function can return a pointer.
program based on pointers
Example 9 : write a program using pointers to compare two strings.
#include<stdio.h>
#include<conio.h>
211
Pointers
214
Pointers
void reverse(char*);
main()
{
char string[100];
printf(“Enter a string\n”);
gets(string);
reverse(string);
printf(“Reverse of entered string is \”%s\”.\n”, string);
return 0;
}
void reverse(char *string)
{
int length, c;
char *begin, *end, temp;
length = string_length(string);
begin = string;
end = string;
for ( c = 0 ; c < ( length - 1 ) ; c++ )
end++;
for ( c = 0 ; c < length/2 ; c++ )
{
temp = *end;
*end = *begin;
*begin = temp;
begin++;
end—;
}
}
int string_length(char *pointer)
{
int c = 0;
while( *(pointer+c) != ‘\0’ )
215
Pointers
c++;
return c;
}
O/P:
Enter a string hello
Reverse of entered string is “olleh”
Example13 : write a c program to find whether a given string is palindrome or not by using
pointers.
#include <stdio.h>
int is_palindrome(char*);
void copy_string(char*, char*);
void reverse_string(char*);
int string_length(char*);
int compare_string(char*, char*);
main()
{
char string[100];
int result;
printf(“Enter a string\n”);
gets(string);
result = is_palindrome(string);
if ( result == 1 )
printf(“\”%s\” is a palindrome string.\n”, string);
else
printf(“\”%s\” is not a palindrome string.\n”, string);
return 0;
}
int is_palindrome(char *string)
{
int check, length;
char *reverse;
length = string_length(string);
216
Pointers
reverse = (char*)malloc(length+1);
copy_string(reverse, string);
reverse_string(reverse);
check = compare_string(string, reverse);
free(reverse);
if ( check == 0 )
return 1;
else
return 0;
}
int string_length(char *string)
{
int length = 0;
while(*string)
{
length++;
string++;
}
return length;
}
void copy_string(char *target, char *source)
{
while(*source)
{
*target = *source;
source++;
target++;
}
*target = ‘\0’;
}
void reverse_string(char *string)
{
int length, c;
217
Pointers
218
Pointers
8.13 Summary
In this unit we have studied about pointers, pointer arithmetic, passing pointers to
functions, relation to arrays and the concept of dynamic memory allocation. A pointer is simply
a variable that contains an address which is a location of another variable in memory. The
unary operator &, when preceded by any variable returns its address. C’s other unary pointer
operator is *, when preceded by a pointer variable returns a value stored at that address.
Pointers are often passed to a function as arguments by reference. This allows data
items within the calling function to be accessed, altered by the called function, and then returned
to the calling function in the altered form. There is an intimate relationship between pointers
219
Pointers
and arrays as an array name is really a pointer to the first element in the array. Access to the
elements of array using pointers is enabled by adding the respective subscript to the pointer
value (i.e. address of zeroth element) and the expression preceded with an indirection operator.
As pointer declaration does not allocate memory to store the objects it points at,
therefore, memory is allocated at run time known as dynamic memory allocation. The library
routine malloc can be used for this purpose.
1. Define pointers.
2. What do you understand by null pointer.
3. What do you understand by dynamic memory allocation? Expain different function
used in dynamic memory allocation.
4. Write short notes on pointers to pointers.
5. Write a program to print hello world using pointers.
6. Write a program to subtract two integer values by using pointer.
Reference Link
1. www.programiz.com/c-programming
2. www.tutorialspoint.com/cprogramming
3. www.cprogramming.com
4. en.wikipedia.org/wiki/C_(programming_language)
To declare a structure
Accessing the members of structure
Initializing Structures
220