Advertisement
lalani001

Untitled

Mar 25th, 2023
719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. //Pointers:-Variables That Stores Address of another variable
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.     int a = 5;
  7.     int* p; //p is pointer to integer
  8.     p = &a;// p store address of a
  9.     //cout << *p<<endl; //* this called derefrencing
  10.        //         Pointer Airthmetic
  11.     int b = 10;
  12.     int* ptr = &b;
  13.     //cout << ptr<<endl;//address of b
  14.     //cout << ptr + 1 << endl;//address of b+1 means 4bytes aage ka address par jaye ga
  15.     //Void Pointer
  16.     void* po;
  17.     po = &b; //we can't dereference or pointer Airthmeic
  18.     //Pointer To Pointer
  19.     int x = 5;
  20.     int* pt = &x;//pointer to integer
  21.     *pt = 6;
  22.     int** q =& pt;//pointer to pointer to integer
  23.     int*** r = &q;//pointer to pointer to poiner to integer
  24.     cout << *pt << endl;//6
  25.     cout << *q << endl;//Address of x
  26.     cout << &x << endl;//Address of x
  27.     cout << *(*q) << endl;//6
  28.     cout << *(*r) << endl;//Address of x
  29.     cout << *(*(*r)) << endl;//Value of x 6
  30.     cout << r << endl;//Address of q
  31.     cout << &q << endl;//Address of q
  32.     cout << *r << endl;//Address of pt
  33.     cout << &pt << endl;//Address of pt
  34.     //            Pointers And Arrays
  35.     //Arrays is a collection of elements of the same data type ,store in contiguous memory locations
  36.     int A[5] = { 2,4,5,8,1 };
  37.     int* ptrArr;
  38.     ptrArr = A;//OR p = &A[0] It will store Address of first element of array
  39.     cout << "Pointers And Arrays"<<endl;
  40.     cout << A<<endl;//Address of A OR A[0] OR (A+0)
  41.     cout << *A << endl;//Value At 0 index :-2
  42.     //When we pass array as function arguments so it will pass as call by reference means so the address of arrays goes not values of array
  43.     //Ex
  44.     //int sumOfArray(int A[]) OR (int* A)
  45.     //              Character Arrays And Pointers
  46.     //How To Store Strings
  47.     //size of array>= no of character in string+1
  48.     //Ex Like "John" size>=5  one extra space of null teriminated character '\0'
  49.     cout << "Character Array And pointer"<<endl;
  50.     char c[8];
  51.     c[0] = 'j';
  52.     c[1] = 'o';
  53.     c[2] = 'h';
  54.     c[3] = 'n';
  55.    
  56.     cout << c<<endl;//without Null Terminated  '\0'
  57.     c[4] = '\0';
  58.     cout << c << endl;//with Null Terminated  '\0'
  59.     //We can Write Like This
  60.     char ch[20] = "john";
  61.     char chh[] = "john";
  62.     char chhh[] = { 'j','o','h','n' };
  63.     cout << ch << endl;
  64.     cout << chh << endl;
  65.     cout << chh << endl;
  66.     //Arrays and Pointers Are diffreent types that are used in similar manner
  67.     char c1[6] = "hello";
  68.     char* c2;
  69.     c2 = c1;
  70.     cout << c2[1]<<endl;//e
  71.     c2[0] = 'A';// "Aello";
  72.     cout << c2<<endl;
  73.     //c2[i] OR *(c2+i)
  74.     cout << *(c2 + 1)<<endl;
  75.     //c1=c2;(Wrong) , c1=c1+1;(Wrong),c2++;(right)
  76.     // char* st= "hlw";his error occurs because the string literal "hlw" is a constant string,
  77.     //which is stored in read-only memory. When you declare a pointer to this string using the statement:
  78.     const char* st = "hlw";
  79.     //        Pointers And Multi-dimensional Array
  80.  
  81.  
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement