0% found this document useful (0 votes)
20 views

Lecture 5

1) Pointers are a core component of C programming that allow storing and manipulating data in memory locations. 2) There are three main steps to using pointers: declaration, initialization, and dereferencing. 3) Pointers can be classified into different types like integer pointers, array pointers, structure pointers, and more. Pointer arithmetic allows performing operations like incrementing, decrementing, adding/subtracting integers to pointers.

Uploaded by

Keshav Chauhan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lecture 5

1) Pointers are a core component of C programming that allow storing and manipulating data in memory locations. 2) There are three main steps to using pointers: declaration, initialization, and dereferencing. 3) Pointers can be classified into different types like integer pointers, array pointers, structure pointers, and more. Pointer arithmetic allows performing operations like incrementing, decrementing, adding/subtracting integers to pointers.

Uploaded by

Keshav Chauhan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

1

Lecture #5

Introduction to Pointers

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


2 Pointers in data structure?

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.

Syntax datatype *ptr;

ptr is the name of the pointer.


datatype is the type of data it is pointing to.

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


3 How to Use Pointers?

The use of pointers can be divided into three steps:

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.

Example int *ptr;

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;

We can also declare and initialize the pointer in a single step.

int *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.

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


5 3. Dereferencing

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.

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


6 Pointer in C++ - Example

#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

// assign the address of a variable to a pointer Value at var = 10


cout<<“Value of ptr”<<ptr; Value at *ptr = 10
cout<<“Value of var”<<var;
cout<<“Value of *ptr”<<*ptr;
}

int main()
{
myFunction();
return 0;
}

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


7 Types of Pointers

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

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


8 1. Integer Pointers

As the name suggests, these are the pointers that point to the integer values.

Syntax int *ptr;

These pointers are pronounced as Pointer to Integer.

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.

char *ptr = &array_name;

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


9 3. Structure Pointer

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

Syntax struct struct_name *ptr;

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.

Syntax datatype ** pointer_name;

Dereferencing Double Pointer


*pointer_name; // get the address stored in the inner level pointer
**pointer_name; // get the value pointed by inner level pointer
© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh
10
5. NULL 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.

Syntax data_type *pointer_name = NULL;


or
pointer_name = NULL

It is said to be good practice to assign NULL to the pointers currently not in use.

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


11 6. Void Pointer
The Void pointers in C are the pointers of type void. It means that they do not have any associated data
type. They are also called generic pointers as they can point to any type and can be type casted to any
type.

Syntax: void * pointer_name;


One of the main properties of void pointers is that they cannot be dereferenced.
7. Wild Pointers

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;

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


12 8. Constant Pointers

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.

Syntax const data_type * pointer_name;

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.

Syntax data_type * const pointer_name;

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


13 Pointer Arithmetic

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.

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


14 Program to illustrate Pointer Arithmetic

#include <iostream.h> Output


int main()
{ Value of *ptr = 10
int v[3] = { 10, 100, 200 }; // Declare an array Value of ptr = 0x7ffe8ba7ec50
int* ptr; // Declare pointer variable
ptr = &v; // Assign the address of v[0] to ptr Value of *ptr = 100
for (int i = 0; i < 3; i++) { Value of ptr = 0x7ffe8ba7ec54
// print value at address which is stored in ptr
cout<<"Value of *ptr:"<<*ptr<<endl; Value of *ptr = 200
// print value of ptr Value of ptr = 0x7ffe8ba7ec58
cout<<"Value of ptr:"<<ptr; // Increment
pointer ptr by 1
ptr++;
}
return 0;
}

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


15 Pointers and Arrays

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.

Example 1: Accessing Array Elements using Pointer with Array Subscript

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


16 Program:

#include <iostream.h> Output


Elements of the array are: 5 10 15
void myFunction()
{
int val[3] = { 5, 10, 15 };
int* ptr; // Declare pointer variable

ptr = &val;
cout<<"Elements of the array are: ";
cout<<*ptr[0]<<*ptr[1]<< *ptr[2];
return;
}
int main()
{
myFunction();
return 0;
}

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


17 Example 2: Accessing Array Elements using Pointer Arithmetic

#include <iostream.h> Output


int main() 12345
{
int arr[5] = { 1, 2, 3, 4, 5 };
// defining the pointer to array
int *ptr_arr = &arr;

// traversing array using pointer arithmetic


for (int i = 0; i < 5; i++) {
cout<<*ptr_arr++;
}
return 0;
}
© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh
18 Advantages of Pointers

Following are the major advantages of pointers in C:

 Pointers are used for dynamic memory allocation and deallocation.


 An Array or a structure can be accessed efficiently with pointers
 Pointers are useful for accessing memory locations.
 Pointers are used to form complex data structures such as linked lists, graphs, trees, etc.
 Pointers reduce the length of the program and its execution time as well.

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


19 Disadvantages of Pointers

Pointers are vulnerable to errors and have following disadvantages:

 Memory corruption can occur if an incorrect value is provided to pointers.


 Pointers are a little bit complex to understand.
 Pointers are majorly responsible for memory leaks in C.
 Pointers are comparatively slower than variables in C.
 Uninitialized pointers might cause a segmentation fault.

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


20

Thank You

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh

You might also like