Lecture 3.1.1 Basics of Pointers
Lecture 3.1.1 Basics of Pointers
Course Objectives
CO5 Design and develop modular programs for real world problems us-
ing control structure and selection structure.
3
ASSESSMENT PATTERN
The performance of students is evaluated as follows:
Theory Practical
Continuous Internal As- Semester End Examina- Continuous Internal Semester End Exam-
Components sessment (CAE) tion (SEE) Assessment (CAE) ination (SEE)
Marks 40 60 60 40
Total Marks 100 100
4
Introduction
• POINTER is a variable that stores address
of another variable.
• A pointer can also be used to refer to
another pointer function.
• A pointer can be
incremented/decremented, i.e., to point
to the next/ previous memory location.
• The purpose of pointer is to save Source: https://www.geeksforgeeks.org/double-pointer-pointer-pointer-c/
memory space and achieve faster
execution time.
5
Features of Pointers
• Pointers save memory space.
• Execution time with pointers is faster because data are manipulated with the address,
that is, direct access to memory location.
• Memory is accessed efficiently with the pointers. The pointer assigns and releases the
memory as well. Hence it can be said the Memory of pointers is dynamically allocated.
• Pointers are used with data structures. They are useful for representing two-
dimensional and multi-dimensional arrays.
• An array, of any type can be accessed with the help of pointers, without considering its
subscript range.
• Pointers are used for file handling.
• Pointers are used to allocate memory dynamically.
• In C++, a pointer declared to a base class could access the object of a derived class.
However, a pointer to a derived class cannot access the object of a base class.
6
Declaring a pointer
• Like variables, pointers have to be declared before they can be used in
your program.
• Pointers can be named anything you want as long as they obey C's
naming rules.
• A pointer declaration has the following form.
data_type * pointer_variable_name;
Here,
• data_type is the pointer's base type of C's variable types and indicates the
type of the variable that the pointer points to.
• The asterisk (*: the same asterisk used for multiplication) which is
indirection operator, declares a pointer.
7
Initialize a pointer
• After declaring a pointer, we initialize it like standard variables with a variable
address.
• If pointers are not uninitialized and used in the program, the results are
unpredictable and potentially disastrous.
• To get the address of a variable, we use the ampersand (&)operator, placed
before the name of a variable whose address we need.
• Pointer initialization is done with the following syntax:
pointer = &variable;
8
Example:
#include <stdio.h>
int main()
{
int a=10; //variable declaration
int *p; //pointer variable declaration
p=&a; //store address of variable a in pointer p
printf("Address stored in a variable p is:%x\n",p); //accessing the address
printf("Value stored in a variable p is:%d\n",*p); //accessing the value
return 0;
}
Output:
Address stored in a variable p is:60ff08
Value stored in a variable p is:10
9
Operators used with Pointers
Operator Meaning
10
Types of a pointer
1. Null pointer
We can create a null pointer by assigning null value during the pointer declaration. This method is
useful when you do not have any address assigned to the pointer. A null pointer always contains value
0.
Following program illustrates the use of a null pointer:
#include <stdio.h>
int main()
{
int *p = NULL; //null pointer
printf(“The value inside variable p is:\n%x”,p);
return 0;
}
Output:
The value inside variable p is:
0
11
2. Void Pointer
In C programming, a void pointer is also called as a generic pointer. It does not have
any standard data type. A void pointer is created by using the keyword void. It can
be used to store an address of any variable.
Output:
The size of pointer is:4
12
3. Wild pointer
A pointer is said to be a wild pointer if it is not being initialized to anything. These
types of pointers are not efficient because they may point to some unknown memory
location which may cause problems in our program and it may lead to crashing of the
program. One should always be careful while working with wild pointers.
Following program illustrates the use of wild pointer:
#include <stdio.h>
int main()
{
int *p; //wild pointer
printf("\n%d",*p);
return 0;
}
Output:
timeout: the monitored command dumped core
sh: line 1: 95298 Segmentation fault timeout 10s main
13
Uses of pointers
• To pass arguments by reference
• For accessing array elements
• To return multiple values
• Dynamic memory allocation
• To implement data structures
• To do system level programming where memory addresses are useful
14
Disadvantages of pointer
• Uninitialized pointers might cause segmentation fault.
• Dynamically allocated block needs to be freed explicitly.
• If pointers are updated with incorrect values, it might lead to memory
corruption.
15
Summary
A pointer can be
POINTER is a variable that A pointer can also be used
incremented/decremented,
stores address of another to refer to another pointer
i.e., to point to the next/
variable. function.
previous memory location.
16
Frequently Asked question
Q1:2) What will be the output of the C program?
#include<stdio.h>
int main(){
int i = 3;
int *j;
j = &i;
j++;
printf("%d ",*j);
return 0;
}
Output:
17
2) What will be the output of the C program?
#include<stdio.h>
int main(){
int a = 130;
char *ptr;
ptr = (char *)&a;
printf("%d ",*ptr);
return 0;
}
Ans : -126
18
• Output:
19
Q3) What is “&” and “*” operators in C?
Ans:
1. “*” Operator is used as pointer to a variable. Example: * a where * is
pointer to the variable a.
2. & operator is used to get the address of the variable. Example: &a will give
address of a.
20
Assessment Questions:
1. C Program to calculate Area of Circle using Pointer
• Write a program to count the number of words, lines and characters in a text using
pointer
22
REFERENCES
Reference Books
[1] Programming in C by Reema Thareja.
[2] Programming in ANSI C by E. Balaguruswamy, Tata McGraw Hill.
[3] Programming with C (Schaum's Outline Series) by Byron Gottfried Jitender
Chhabra, Tata McGraw Hill.
[4] The C Programming Language by Brian W. Kernighan, Dennis Ritchie, Pearson
education.
Websites:
1. https://www.geeksforgeeks.org/features-and-use-of-pointers-in-c-c/
2. https://www.guru99.com/c-pointers.html
YouTube Links:
3. https://www.youtube.com/watch?v=sY-s7O0FiYE&t=3s
4. https://www.youtube.com/watch?v=qcWQ1AmmNWI&t=193s
23
THANK YOU