Professional Documents
Culture Documents
Unit V Pointers 1
Unit V Pointers 1
Pointers
12 Hours
12 Marks
1 of 18
Normal (or ordinary) variables store values in them. But pointers contain memory
addresses as their values. Pointer is a special variable which holds address of a memory location.
With the help of pointers, memory can be accessed easily. Pointer helps a lot in dynamic
memory allocation.
Following are the benefits of pointers,
- Pointers support dynamic memory management.
- Pointers reduce length and complexity of programs.
- They increase speed of execution. So execution time is reduced.
- Pointer arrays to character strings result in saving of memory space.
- Pointers can be used to return multiple values from a function.
- Pointers also permit references to other functions.
- Pointers are closely associated with arrays. So arrays can be efficiently handled using
pointers.
- Pointers can efficiently handle dynamic data structures like linked lists.
Highest Address -
-
-
11
10
9
8
7
6
5
4
3
2
1
0
Figure 5.1: Memory Organization
Whenever a variable is declared, memory space is allocated to it depending on its data
type. One example is shown below.
float a=27.6;
For variable „a‟, 4 bytes of memory is allocated and 27.6 is stored in the memory cells
allocated to it. Address of first location where a variable gets allocation, is the address of the
variable. So address of variable „a‟ is 2763 as per memory allocation shown in figure 5.2. This is
assumption. Actual memory allocation is taken care by memory management system of
operating system. So it may vary.
2 of 18
Highest Address -
-
-
2771
2770
2769
2768
2767
2766
2765
2764
2763
27.6 a
2762
2761
-
-
-
0
3 of 18
5.1.3 Declaration of pointer variable
Before studying declaration of a pointer variable, let us discuss definition of a pointer.
Definition of Pointer:
Pointer is a special variable which is used for holding address of memory location
where value of a specific data type is stored.
As discussed before, memory requirements are different for storing values of different
data types. So, only holding the address is not sufficient. Program should be able to know that
data is stored in how many bytes from the starting location. For this purpose, specifying data
type in declaring pointer variable is must.
Syntax of declaring a pointer variable is given below.
data-type *pointer-name;
The asterisk is used for telling compiler that this is declaration of a pointer variable. Here
pointer-name is a special variable which can contain address of memory location where value of
specified data-type is stored.
Some examples are discussed below.
int *ip; ip is a pointer variable that can contain address of memory location where
integer value is stored. (i.e. ip is a int pointer)
float *fp; fp is a pointer variable that can contain address of memory location where
float value is stored. (i.e. fp is a float pointer)
double *dp; dp is a pointer variable that can contain address of memory location where
double value is stored. (i.e. dp is a double pointer)
char *cp; cp is a pointer variable that can contain address of memory location where
char value is stored. (i.e. cp is a char pointer)
Like any other variable, when a pointer variable is declared memory space (2 bytes) is
allocated to it and it contains a random value (i.e. newly declared pointer variable will point to
any memory location randomly).
4 of 18
Valid Example 1:
int a;
int *ptr;
ptr=&a;
Valid Example 2:
float x,*fp;
fp=&x;
Valid Example 3:
/* We can also set initial value of a pointer as NULL (i.e. „\0‟ ) */
int *ip=NULL; /* OR int *ip=‟\0‟ */
We may also have chain of pointers up to any depth. i.e. we may have pointer which is
pointing to another pointer and so on and the last pointer is pointing to a memory location where
value of specified data type is stored. This is also called as multiple indirections. Some examples
of chain of pointers are discussed here.
5 of 18
Example 1:
float **p;
float *fp,b;
b=10.42;
fp=&b; /* Address of b gets stored in fp */
p=&fp; /* Address of fp gets stored in p */
/* Here p is pointer to pointer */
/* We can access value of b by using b, *fp or **fp */
Example 2:
int ***r,**q,*p,i=7;
p=&i; /* Address of i gets stored in p */
q=&p; /* Address of p gets stored in q */
r=&q; /* Address of q gets stored in r */
printf(“%d %d %d”,*p,**q,***r);
/* In above printf statement, *p displays contents of memory location pointed by
p. i.e. value of variable i (i.e. 7). **q displays contents of memory location
pointed by pointer whose address is stored in q. ***r displays contents of memory
location pointed by pointer whose address is stored in another pointer pointed by
r. */
/* So, the output of above code is 7 7 7 */
Similar to other variables, pointers can be used in expressions. If ip is a pointer, *ip gives
contents of memory location pointed by ip.
Following are some valid examples by considering ptr and pntr as two pointer variables
of same data types (obviously, these pointers must be properly initialized).
res = *ptr + *pntr; /* addition of values of memory locations pointed by ptr
and pntr gets stored in res */
b = a – *pntr; /* value of subtraction of a and value of memory location
pointed by *pntr gets stored in b */
Care must be taken while using division operator along with pointer. As „/*‟ is treated as
beginning of a comment, space should be given in between „/‟ and „*‟ operators as shown in
following example.
res = *ptr / *pntr;
/* It should not be written as res = *ptr /*pntr */
6 of 18
Whenever above arithmetic operations are performed on pointers, scale factor is used
automatically. Scale factor is nothing but number of bytes used for storing value of specific data
type. Some examples are discussed below.
Example 1:
int *ip, a[10], i;
ip=a; /* Address of first element of array a gets stored in ip */
for(i=0;i<10;i++)
{
printf(“ %d”,*ip);
ip++;
/* Here ip will be incremented by 2 if size of int data type is 2 */
/* So, individual array element is displayed in each iteration */
}
Example 2:
int *ip, a[10], i;
ip=&a[9];
/* Address of last element of array a (i.e. a[9]) gets stored in ip */
printf(“ %d”,*ip); /* Displays value of a[9] */
ip = ip – 3; /* 3 (multiplied by scale factor) gets subtracted from ip */
printf(“ %d”, *ip); /* Displays value of a[6] */
Relational operators like <, <=, >, >=, == and != can also be used for comparing two
pointers. This comparison is meaningful in case of arrays and strings only.
7 of 18
ip+1 is equivalent to &a[1] which is 2414
-
ip+8 is equivalent to &a[8] which is 2428
ip+9 is equivalent to &a[9] which is 2430
2428 a[8]
2426 a[7]
2424 a[6]
2422 a[5]
2420 a[4]
2418 a[3]
2416 a[2]
2414 a[1]
2412 a[0]
-
0 Memory Location -
Figure 5.3: Sample Memory Allocation for array a
As pointer is equivalent to array a, we can use it for accessing individual elements of
array. Some examples are shown below.
*ip gives value of a[0]
*(ip+1) gives value of a[1]
*(ip+2) gives value of a[2]
*(ip+3) gives value of a[3]
-
-
*(ip+8) gives value of a[8]
*(ip+9) gives value of a[9]
Pointer accessing method is faster than accessing the elements of array by using index of
array.
As an example, code for storing squares of given list (array) of elements into another
array is given below. Different logics are used for reading array elements and for displaying
squares.
pa=a;
pb=b;
printf(“Enter five values:\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,(pa+i));
*(pb+i)=*(pa+i) * (*(pa+i));
/* Here, *(pb+i) represents value of b[i] element and *(pa+i)
represents value of a[i] element */
}
Sample Output:
Enter five values:
26937
The square values of entered values are:
4 36 81 9 49
9 of 18
Example 2:
char *name;
name=“Shubman Gill”;
/* This is an example of run-time assignment */
When we declare stings using pointers, only necessary memory gets allocated. So,
wastage of memory is avoided.
Pointers can be used to access individual characters in a string. Example code is shown
below.
ptr2=str;
while(*ptr2!=‟\0‟)
{
ptr2++;
}
ptr2--; /* ptr2 points to last character of str */
while(ptr1<ptr2)
{
if(*ptr1==*ptr2)
{
ptr1++;
ptr2--;
}
else
{
break;
}
}
if(ptr1<ptr2)
{
printf(“%s is palindrome”,str);
}
else
{
printf(“%s is not palindrome”,str)l
}
10 of 18
5.5 Handling functions using Pointers
We can also have pointer to function. In such case, pointer is a special variable which is
used for holding address of memory location where function of a specific prototype is stored.
As discussed before, pointer to integer can hold address of memory location where
integer value is stored. It cannot hold address of float variable. Similarly, a pointer to function
can hold address of only the functions whose prototype is matching with the pointer declaration.
Example Program 1:
11 of 18
}
main( )
{
int x,y,z;
int (*ptr)(int, int);
ptr = add;
z = ptr(x,y);
Output:
Enter two numbers: 56 72
Addition of 56 and 72 is 128
Example Program 2:
main( )
{
int x,y,z;
int (*ptr)(int, int);
ptr = add;
12 of 18
z = ptr(x,y);
ptr = mul;
z = ptr(x,y);
Output:
Enter two numbers: 56 72
Addition of 56 and 72 is 128
Multiplication of 56 and 72 is 4032
struct structure-name
{
data-type var1;
--
--
} *pointer-name;
13 of 18
};
For accessing members of structure through a pointer variable, arrow operator (->) is
used. For above examples we may access the members as follows.
ptr->roll_no
ptr->name
p->emp_id
p->name
Some example programs for handling structures using pointers are shown below.
Example Program 1:
main( )
{
struct student
{
int roll_no;
char name[15];
};
14 of 18
printf(“Data you entered is as follows:\n”);
printf(“Roll Number of Student: %d\n”,ptr->roll_no);
printf(“Name of Student: %s”,ptr->name);
}
Output:
Enter roll number of student: 34
Enter name of student: Sanjivani
Data you entered is as follows:
Roll Number of Student: 34
Name of Student: Sanjivani
Example Program 2:
main( )
{
int i;
struct employee
{
int emp_no;
char name[15];
}e[3],*p;
/* e1 is array of three employees and p is pointer to structure */
15 of 18
Output:
Enter data of three employees:
Enter employee ID of employee: 5222
Enter name of employee: Ramesh
Enter employee ID of employee: 1612
Enter name of employee: Ayush
Enter employee ID of employee: 1213
Enter name of employee: Anay
Sample Questions
1. Define pointer. Write syntax for pointer declaration. [2M]
2. State syntax to declare pointer variable with example. [2M]
3. Give any four advantages of pointers. [2M]
4. What is pointer? Give its declaration. [2M]
5. What is pointer? Give one example of integer and character pointer. [2M]
6. Define pointer variable. What are advantages of pointer? [2M]
7. List advantages of using pointer. [2M]
8. State use of '&' and '*' operators with respect to pointer. [2M]
9. State limitations of pointer. [2M]
10. Write output of following program. [2M]
void main( )
{
int ***r,**q,*p,i=7;
p=&i;
q=&p;
r=&q;
printf(“%d%d%d”,*p,**q,***r);
}
11. What is pointer? How is it declared? How is it used? Explain with suitable example [4M]
12. With suitable example, list different pointer arithmetic operations. [4M]
13. Write a program to find length of string using pointers. [4M]
14. Write a program in C using pointer to determine length of a string. [4M]
15. Write a program to find whether string is palindrome (using pointer). [4M]
16. Define pointer. State and describe pointer operators. [4M]
17. List different pointer arithmetic operations. Give example of each. [4M]
16 of 18
18. Explain pointer arithmetic with example. [4M]
19. Explain how array elements are accessed using pointers. [4M]
20. Write a program in C using pointers to compute the sum of elements stored in an array
abc[ ]. [4M]
21. Explain how pointers are used in call-by-reference method of function. [4M]
22. Write a program to print first ten terms of Fibonacci series using pointers [4M]
23. Write the values of d in following component of program. [4M]
int a=20;
int *b=&a;
int *c=b;
int d=*b+*c;
24. State meaning of following statements with respect to pointer. [4M]
int *ptr,m=8;
*ptr=m;
ptr=&m;
25. Explain meaning of following statements with reference to pointers. [4M]
int *ptr,m=10;
ptr=m;
ptr=&m;
26. Explain when to use pointers in a program and give examples of pointer expressions that
can be used in any application program. [4M]
27. Explain meaning of following statements with reference to pointers. [4M]
int *p,x;
x=10;
*p=x;
p=&x;
28. Explain the concept of pointers arithmetic operators with example. [4M]
29. How pointers are declared and accessed? [4M]
30. Explain the concept of pointer arithmetic. [4M]
31. Explain the concept of pointer to array. Give example. [4M]
32. Explain meaning of following statement with reference to pointers. [4M]
int *a,b;
b=20;
*a=b;
a=&b;
33. Write a program to print reverse of entered string using pointer. [6M]
34. Write a program to print values of variables and their addresses. [6M]
35. Write a program to accept a string as input from user and determine its length. [Don‟t use
built-in library function strlen( )] [4M]
36. Write a program to accept two numbers from user and perform addition, subtraction,
multiplication and division operations using pointer. [6M]
37. Explain how to pass pointer to function with example. [4M]
38. Write a program to swap values of variables a=10 and b=5 using function. [4M]
39. Develop a program to swap two numbers using pointer, add swapped numbers and print
their addition. [6M]
40. Distinguish/Differentiate between call by value and call by reference. [2M/4M]
17 of 18
41. Explain pointer arithmetic with example. [4M]
42. State four arithmetic operations performed on pointer with example. [4M]
43. Write output of following C program. [4M]
#include<stdio.h>
int main( )
{
char *ptr;
char str[ ]=“MAHARASHTRA STATE BOARD OF TECHNICAL
EDUCATION”;
ptr=str;
ptr=ptr+11;
printf(“%s”,++ptr);
return 0;
}
18 of 18