SlideShare a Scribd company logo
POINTER
 POINTER is a variable that holds the memory address of another variable.
 Ex: int a =10;
 Int* ptr=&a;
 Cout<<a; // a=10
 Cout<<ptr; //ptr=1000
 Cout<< *ptr;//*ptr=10
 Cout<<&a; //a=1000
 While working with operator we need two operator
 & address of operator
 * value at address operator
 Write a C++ program that declares an integer variable, initializes it with a
value, and then uses a pointer to change the value of that variable.
 #include <iostream.h>
 #include <conio.h>
 int main() {
 clrscr(); // Clear the screen
 int number = 42; // Declare and initialize an integer variable
 int *ptr; // Declare a pointer to an integer
 ptr = &number; // Assign the address of 'number' to the pointer 'ptr'
 cout << "Original value of 'number': " << number << endl;
 // Use the pointer to change the value of 'number'
 *ptr = 99;
 cout << "New value of 'number' using pointer: " << number << endl;
 getch(); // Wait for a key press to close the console
 return 0;
 }
 Write a C++ program to find the sum of elements in an array using pointer.
 #include <iostream.h>
 #include <conio.h>
 int main() {
 clrscr(); // Clear the screen
 int n;
 cout << "Enter the number of elements in the array: ";
 cin >> n;
 if (n <= 0) {
 cout << "Invalid input. Please enter a positive number of elements." << endl;
 }
 int arr[100]; // Assuming a maximum of 100 elements, adjust as needed
 int *ptr = arr; // Pointer to the array
 int sum = 0;
 cout << "Enter the elements of the array:" << endl;
 // Input the elements of the array
 for (int i = 0; i < n; i++) {
 cin >> *ptr;
 sum += *ptr;
 ptr++;
 }
 cout << "Sum of elements in the array: " << sum << endl;
 getch(); // Wait for a key press to close the console
 return 0;
 }
 Create a function to find the square of a number using pointer
 #include <iostream.h>
 #include <conio.h>
 // Function to calculate the square of a number using pointers
 void square(int *ptr) {
 *ptr = (*ptr) * (*ptr);
 }
 int main() {
 clrscr(); // Clear the screen
 int number;
 cout << "Enter a number: ";
 cin >> number;
 // Call the square function to calculate the square using a pointer
 square(&number);
 cout << "Square of the number: " << number << endl;
 getch(); // Wait for a key press to close the console
 return 0;
 }
 Create a function to swap two numbers using pointers.
 #include <iostream.h>
 #include <conio.h>
 // Function to swap two numbers using pointers
 void swapNumbers(int *ptr1, int *ptr2) {
 int temp = *ptr1; // Store the value pointed to by ptr1 in a temporary variable
 *ptr1 = *ptr2; // Assign the value pointed to by ptr2 to ptr1
 *ptr2 = temp; // Assign the value stored in the temporary variable to ptr2
 }
 int main() {
 clrscr(); // Clear the screen
 int num1 = 10;
 int num2 = 20;
 cout << "Before swapping: num1 = " << num1 << ", num2 = " << num2 << endl;
 // Call the swapNumbers function to swap the values of num1 and num2 using pointers
 swapNumbers(&num1, &num2);
 cout << "After swapping: num1 = " << num1 << ", num2 = " << num2 << endl;
 getch();
 return 0;
 What is basic difference between call by value and call by reference .
 any changes made to the parameter inside the function do not affect the
original data outside the function.
 Call by value is suitable for situations where you want to protect the original
data from being modified within the function.
 //Pass by value
 void modifyValue(int x) {
 x = x * 2; // Modifications to x don't affect the original value outside the
function
 }
 int main() {
 int num = 5;
 modifyValue(num);
 // num is still 5
 return 0;
 }
 // pass by reference
 void modifyReference(int &x) {
 x = x * 2; // Modifications to x directly affect the original value
 }
 int main() {
 int num = 5;
 modifyReference(num); // num is now 10
 return 0;
 }
 create a function to swap two integer values using call by value
 #include<iostream.h>
 #include<conio.h> // Function to swap two integers using Call by Value
 void swapByValue(int a, int b) {
 int temp = a;
 a = b;
 b = temp;
 }
 void main() {
 clrscr(); // Clear the screen
 int num1 = 5;
 int num2 = 10;
 cout << "Before swapping:" << endl;
 cout << "num1 = " << num1 << endl;
 cout << "num2 = " << num2 << endl;
 // Call swapByValue function
 swapByValue(num1, num2);
 cout << "After swapping by value:" << endl;
 cout << "num1 = " << num1 << endl;
 cout << "num2 = " << num2 << endl;
 getch(); // Wait for a key press before exiting
 }
 create a function to swap two integer values using call by reference.
 #include<iostream.h>
 #include<conio.h>
 // Function to swap two integers using Call by Reference
 void swapByReference(int &a, int &b) {
 int temp = a;
 a = b;
 b = temp;
 }
 void main() {
 clrscr(); // Clear the screen
 int num1, num2;
 cout << "Enter the first integer: ";
 cin >> num1;
 cout << "Enter the second integer: ";
 cin >> num2;
 cout << "Before swapping:" << endl;
 cout << "num1 = " << num1 << endl;
 cout << "num2 = " << num2 << endl;
 // Call swapByReference function
 swapByReference(num1, num2);
 cout << "After swapping by reference:" << endl;
 cout << "num1 = " << num1 << endl;
 cout << "num2 = " << num2 << endl;
 getch();
 }

More Related Content

PDF
C++ Nested loops, matrix and fuctions.pdf
PPTX
C++ FUNCTIONS-1.pptx
PDF
Pointer in C++_Somesh_Kumar_Dewangan_SSTC
DOCX
C++ file
DOCX
C++ file
PDF
Please use the code below and make it operate as one program- Notating.pdf
PDF
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
PPT
ch5_additional.ppt
C++ Nested loops, matrix and fuctions.pdf
C++ FUNCTIONS-1.pptx
Pointer in C++_Somesh_Kumar_Dewangan_SSTC
C++ file
C++ file
Please use the code below and make it operate as one program- Notating.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
ch5_additional.ppt

Similar to Pointers in c++ programming presentation (20)

DOCX
EJERCICIOS DE BORLAND C++ 2DA PARTE.docx
DOCX
EJERCICIOS DE BORLAND C++ 2DA PARTE.docx
PDF
C++ TUTORIAL 4
PDF
how to reuse code
PPT
Lecture2.ppt
PPT
Computer Programming- Lecture 7
PDF
4th_Ed_Ch03.pdf
PPT
C++ Functions.ppt
PPTX
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
PPTX
Object Oriented Programming using C++: Ch10 Pointers.pptx
PDF
C++ TUTORIAL 3
PDF
I have written the code but cannot complete the assignment please help.pdf
PPTX
CSC2161Programming_in_Cpp_Lecture notes.pptx
PPT
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
PPT
FP 201 - Unit 6
DOCX
#include iostreamusing namespace std;double sumNums(
PPTX
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
PPTX
C++ programming function
PPT
CPP Language Basics - Reference
EJERCICIOS DE BORLAND C++ 2DA PARTE.docx
EJERCICIOS DE BORLAND C++ 2DA PARTE.docx
C++ TUTORIAL 4
how to reuse code
Lecture2.ppt
Computer Programming- Lecture 7
4th_Ed_Ch03.pdf
C++ Functions.ppt
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Object Oriented Programming using C++: Ch10 Pointers.pptx
C++ TUTORIAL 3
I have written the code but cannot complete the assignment please help.pdf
CSC2161Programming_in_Cpp_Lecture notes.pptx
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
FP 201 - Unit 6
#include iostreamusing namespace std;double sumNums(
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
C++ programming function
CPP Language Basics - Reference
Ad

Recently uploaded (20)

PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Well-logging-methods_new................
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
web development for engineering and engineering
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Sustainable Sites - Green Building Construction
PPTX
Foundation to blockchain - A guide to Blockchain Tech
Internet of Things (IOT) - A guide to understanding
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Well-logging-methods_new................
Lecture Notes Electrical Wiring System Components
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CH1 Production IntroductoryConcepts.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
web development for engineering and engineering
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Sustainable Sites - Green Building Construction
Foundation to blockchain - A guide to Blockchain Tech
Ad

Pointers in c++ programming presentation

  • 2.  POINTER is a variable that holds the memory address of another variable.  Ex: int a =10;  Int* ptr=&a;  Cout<<a; // a=10  Cout<<ptr; //ptr=1000  Cout<< *ptr;//*ptr=10  Cout<<&a; //a=1000
  • 3.  While working with operator we need two operator  & address of operator  * value at address operator
  • 4.  Write a C++ program that declares an integer variable, initializes it with a value, and then uses a pointer to change the value of that variable.  #include <iostream.h>  #include <conio.h>  int main() {  clrscr(); // Clear the screen  int number = 42; // Declare and initialize an integer variable  int *ptr; // Declare a pointer to an integer  ptr = &number; // Assign the address of 'number' to the pointer 'ptr'  cout << "Original value of 'number': " << number << endl;  // Use the pointer to change the value of 'number'  *ptr = 99;  cout << "New value of 'number' using pointer: " << number << endl;  getch(); // Wait for a key press to close the console  return 0;  }
  • 5.  Write a C++ program to find the sum of elements in an array using pointer.  #include <iostream.h>  #include <conio.h>  int main() {  clrscr(); // Clear the screen  int n;  cout << "Enter the number of elements in the array: ";  cin >> n;  if (n <= 0) {  cout << "Invalid input. Please enter a positive number of elements." << endl;  }  int arr[100]; // Assuming a maximum of 100 elements, adjust as needed  int *ptr = arr; // Pointer to the array  int sum = 0;  cout << "Enter the elements of the array:" << endl;  // Input the elements of the array  for (int i = 0; i < n; i++) {  cin >> *ptr;  sum += *ptr;  ptr++;  }  cout << "Sum of elements in the array: " << sum << endl;  getch(); // Wait for a key press to close the console  return 0;  }
  • 6.  Create a function to find the square of a number using pointer  #include <iostream.h>  #include <conio.h>  // Function to calculate the square of a number using pointers  void square(int *ptr) {  *ptr = (*ptr) * (*ptr);  }  int main() {  clrscr(); // Clear the screen  int number;  cout << "Enter a number: ";  cin >> number;  // Call the square function to calculate the square using a pointer  square(&number);  cout << "Square of the number: " << number << endl;  getch(); // Wait for a key press to close the console  return 0;  }
  • 7.  Create a function to swap two numbers using pointers.  #include <iostream.h>  #include <conio.h>  // Function to swap two numbers using pointers  void swapNumbers(int *ptr1, int *ptr2) {  int temp = *ptr1; // Store the value pointed to by ptr1 in a temporary variable  *ptr1 = *ptr2; // Assign the value pointed to by ptr2 to ptr1  *ptr2 = temp; // Assign the value stored in the temporary variable to ptr2  }  int main() {  clrscr(); // Clear the screen  int num1 = 10;  int num2 = 20;  cout << "Before swapping: num1 = " << num1 << ", num2 = " << num2 << endl;  // Call the swapNumbers function to swap the values of num1 and num2 using pointers  swapNumbers(&num1, &num2);  cout << "After swapping: num1 = " << num1 << ", num2 = " << num2 << endl;  getch();  return 0;
  • 8.  What is basic difference between call by value and call by reference .  any changes made to the parameter inside the function do not affect the original data outside the function.  Call by value is suitable for situations where you want to protect the original data from being modified within the function.  //Pass by value  void modifyValue(int x) {  x = x * 2; // Modifications to x don't affect the original value outside the function  }  int main() {  int num = 5;  modifyValue(num);  // num is still 5  return 0;  }
  • 9.  // pass by reference  void modifyReference(int &x) {  x = x * 2; // Modifications to x directly affect the original value  }  int main() {  int num = 5;  modifyReference(num); // num is now 10  return 0;  }
  • 10.  create a function to swap two integer values using call by value  #include<iostream.h>  #include<conio.h> // Function to swap two integers using Call by Value  void swapByValue(int a, int b) {  int temp = a;  a = b;  b = temp;  }  void main() {  clrscr(); // Clear the screen  int num1 = 5;  int num2 = 10;  cout << "Before swapping:" << endl;  cout << "num1 = " << num1 << endl;  cout << "num2 = " << num2 << endl;  // Call swapByValue function  swapByValue(num1, num2);  cout << "After swapping by value:" << endl;  cout << "num1 = " << num1 << endl;  cout << "num2 = " << num2 << endl;  getch(); // Wait for a key press before exiting  }
  • 11.  create a function to swap two integer values using call by reference.  #include<iostream.h>  #include<conio.h>  // Function to swap two integers using Call by Reference  void swapByReference(int &a, int &b) {  int temp = a;  a = b;  b = temp;  }  void main() {  clrscr(); // Clear the screen  int num1, num2;  cout << "Enter the first integer: ";  cin >> num1;  cout << "Enter the second integer: ";  cin >> num2;  cout << "Before swapping:" << endl;  cout << "num1 = " << num1 << endl;  cout << "num2 = " << num2 << endl;  // Call swapByReference function  swapByReference(num1, num2);  cout << "After swapping by reference:" << endl;  cout << "num1 = " << num1 << endl;  cout << "num2 = " << num2 << endl;  getch();  }