0% found this document useful (0 votes)
12 views5 pages

Pointers

This lesson plan covers the concept of pointers in programming, explaining their definition, dereferencing, and arithmetic operations. It includes examples of single and double pointers in C++, along with sample programs for calculating the sum of two numbers and finding the first and last digits of a number using pointers. The document provides both theoretical explanations and practical coding exercises.

Uploaded by

mohitpatil2810
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Pointers

This lesson plan covers the concept of pointers in programming, explaining their definition, dereferencing, and arithmetic operations. It includes examples of single and double pointers in C++, along with sample programs for calculating the sum of two numbers and finding the first and last digits of a number using pointers. The document provides both theoretical explanations and practical coding exercises.

Uploaded by

mohitpatil2810
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lesson Plan

Pointers
Q. What is a pointer ?
The pointer is a variable, it is also known as locator or indicator that points to an address of a value. The

symbol of an address is represented by a pointer. In addition to creating and modifying dynamic data

structures, they allow programs to emulate call-by-reference.

For example, *pointer_name

Dereferencing operator(*) : Dereferencing is the method where we are using a pointer to access the element
whose address is being stored. We use the * operator to get the value of the variable from its address.

int a;

// Referencing

int *ptr=&a;

// Dereferencing

int b=*ptr;

Note : Arithmetic such as add, subtract can be performed on pointers as well.

Pointer Increment in C++:

int array[5]={2,4,6,8,11}

int *ptr = array;

ptr++; //pointer increment won’t add 1 to address instead it move to the next immediate

index.

Pointer Decrement in C++ :

int array[5]={1,2,3,4,5}

int *ptr=array;

ptr++;

ptr–; //pointer decrement will decrement to one less index in the array therefore we

have first incremented to the next index so that it might not result in runtime error.

Similarly, pointers can be added or subtracted by any constant value as well.

Double pointers : A pointer stores the memory address of other variables. So, when we define a pointer to

a pointer, the first pointer is used to store the address of the variables, and the second pointer stores the

address of the first pointer. For this very reason, this is known as a Double Pointer or Pointer to Pointer.

For example,

#include <bits/stdc++.h>

using namespace std;

int main(){

int data = 1;

int* pointer1;

int** pointer2;

pointer1 = &data;

pointer2 = &pointer1;

cout << "Value of data :- " <<data << endl;

cout << "Value of data using single pointer :- " <<

Java
C++ &+ DSA
DSA
*pointer1 << endl;

cout << "Value of data using double pointer :- " <<

**pointer2 << endl;

return 0;

Output :
Value of data :- 1

Value of data using single pointer :- 1

Value of data using double pointer :- 1

Q1. Write a program to calculate sum of two numbers using pointers.

#include <iostream>

// Function to calculate the sum of two numbers using pointers

void calculateSum(int* num1, int* num2, int* result) {

*result = *num1 + *num2;

int main() {

int num1, num2, sum;

// Input two numbers

std::cout << "Enter the first number: ";

std::cin >> num1;

std::cout << "Enter the second number: ";

std::cin >> num2;

// Pointers to the numbers and result

int* ptrNum1 = &num1;

int* ptrNum2 = &num2;

int* ptrSum = &sum;

// Call the calculateSum function using pointers

calculateSum(ptrNum1, ptrNum2, ptrSum);

// Print the sum

std::cout << "Sum of " << *ptrNum1 << " and " << *ptrNum2 <<
" is: " << *ptrSum << std::endl;

return 0;

Q2. Write a function to find out the first and last digit of a number without returning anything.

Java
C++ &+ DSA
DSA
#include <iostream>

// Function to find and print the first and last digit of a


number using pointers

void findFirstAndLastDigit(int number, int* firstDigit, int*


lastDigit) {

*lastDigit = number % 10;

// Find the first digit

while (number >= 10) {

number /= 10;

*firstDigit = number;

int main() {

int num, firstDigit, lastDigit;

// Input a number

std::cout << "Enter a number: ";

std::cin >> num;

// Call the function to find and print the first and last
digits using pointers

findFirstAndLastDigit(num, &firstDigit, &lastDigit);

// Print the first and last digits

std::cout << "First Digit: " << firstDigit << std::endl;

std::cout << "Last Digit: " << lastDigit << std::endl;

return 0;

Java
C++ &+ DSA
DSA
THANK

YOU !

You might also like