Pointers
Pointers
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
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;
int array[5]={2,4,6,8,11}
ptr++; //pointer increment won’t add 1 to address instead it move to the next immediate
index.
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.
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>
int main(){
int data = 1;
int* pointer1;
int** pointer2;
pointer1 = &data;
pointer2 = &pointer1;
Java
C++ &+ DSA
DSA
*pointer1 << endl;
return 0;
Output :
Value of data :- 1
#include <iostream>
int main() {
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>
number /= 10;
*firstDigit = number;
int main() {
// Input a number
// Call the function to find and print the first and last
digits using pointers
return 0;
Java
C++ &+ DSA
DSA
THANK
YOU !