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();
 }

Pointers in c++ programming presentation

  • 1.
  • 2.
     POINTER isa 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 workingwith operator we need two operator  & address of operator  * value at address operator
  • 4.
     Write aC++ 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 aC++ 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 afunction 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 afunction 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 isbasic 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.
     // passby 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 afunction 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 afunction 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();  }