Lecture 5
Lecture 5
Lecture #5
Introduction to Pointers
Pointers are one of the core components of the C programming language. A pointer can be used to store
the memory address of other variables, functions, or even other pointers. The use of pointers allows low-
level memory access, dynamic memory allocation, and many other functionality in C.
Definition
A pointer is defined as a derived data type that can store the address of other C variables or a memory
location. We can access and manipulate the data stored in that memory location using pointers.
1. Pointer Declaration
2. Pointer Initialization
3. Dereferencing
1. Pointer Declaration
In pointer declaration, we only declare the pointer but do not initialize it. To declare a pointer, we use the
( * ) dereference operator before its name.
The pointer declared here will point to some random memory address as it is not initialized. Such pointers are
called wild pointers.
© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh
4 2. Pointer Initialization
Pointer initialization is the process where we assign some initial value to the pointer variable. We generally
use the ( & ) addressof operator to get the memory address of a variable and then store it in the pointer
variable.
Example int var = 10;
int *ptr;
ptr = &var;
Note: It is recommended that the pointers should always be initialized to some value before starting using it.
Otherwise, it may lead to number of errors.
Dereferencing a pointer is the process of accessing the value stored in the memory address specified in
the pointer. We use the same ( * ) dereferencing operator that we used in the pointer declaration.
#include <iostream>
void myFunction()
{
int var = 10;
int* ptr; // declare pointer variable
Output
ptr = &var; // note that data type of ptr and var must be same Value at ptr = 0x7fff1038675c
int main()
{
myFunction();
return 0;
}
Pointers can be classified into many different types based on the parameter on which we are defining their
types.
1. Integer Pointers
2. Array Pointer
3. Structure Pointer
4. Double Pointers
5. NULL Pointer
6. Void Pointer
7. Wild Pointers
8. Constant Pointers
9. Pointer to Constant
As the name suggests, these are the pointers that point to the integer values.
2. Array Pointer
Pointers and Array are closely related to each other. Even the array name is the pointer to its first element.
They are also known as Pointer to Arrays. We can create a pointer to an array using the given syntax.
The pointer pointing to the structure type is called Structure Pointer or Pointer to Structure. It can be
declared in the same way as we declare the other primitive data types
4. Double Pointers
we can define a pointer that stores the memory address of another pointer. Such pointers are called
double-pointers or pointers-to-pointer. Instead of pointing to a data value, they point to another pointer.
The Null Pointers are those pointers that do not point to any memory location. They can be created by
assigning a NULL value to the pointer. A pointer of any type can be assigned the NULL value.
It is said to be good practice to assign NULL to the pointers currently not in use.
The Wild Pointers are pointers that have not been initialized with something yet. These types of C-pointers
can cause problems in our programs and can eventually cause them to crash.
Example
int *ptr;
char *str;
In constant pointers, the memory address stored inside the pointer is constant and cannot be modified
once it is defined. It will always point to the same memory address.
9. Pointer to Constant
The pointers pointing to a constant value that cannot be modified are called pointers to a constant. Here we
can only access the data pointed by the pointer, but cannot modify it. Although, we can change the address
stored in the pointer to constant.
The Pointer Arithmetic refers to the legal or valid arithmetic operations that can be performed on a
pointer. It is slightly different from the ones that we generally use for mathematical calculations as
only a limited set of operations can be performed on pointers. These operations include:
1. Increment in a Pointer
2. Decrement in a Pointer
3. Addition of integer to a pointer
4. Subtraction of integer to a pointer
5. Subtracting two pointers of the same type
6. Comparison of pointers of the same type.
7. Assignment of pointers of the same type.
An array name acts like a pointer constant. The value of this pointer constant is the address of the first
element. For example, if we have an array named val then val and &val[0] can be used interchangeably.
ptr = &val;
cout<<"Elements of the array are: ";
cout<<*ptr[0]<<*ptr[1]<< *ptr[2];
return;
}
int main()
{
myFunction();
return 0;
}
Thank You