avr39ripe

cppPointersAdvExamples

Aug 18th, 2021
855
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. void printArr(int arr[], int arrSize)
  4. {
  5.     for (int i{ 0 }; i < arrSize; ++i)
  6.     {
  7.         std::cout << arr[i] << ' ';
  8.     }
  9.     std::cout << '\n';
  10. }
  11.  
  12. void printArr(int arr[], int idxBegin, int idxEnd)
  13. {
  14.     for (int i{ idxBegin }; i < idxEnd; ++i)
  15.     {
  16.         std::cout << arr[i] << ' ';
  17.     }
  18.     std::cout << '\n';
  19. }
  20.  
  21. void printArr(int* begin, int* end)
  22. {
  23.     while (begin != end)
  24.     {
  25.         std::cout << *begin++ << ' ';
  26.     }
  27.     std::cout << '\n';
  28. }
  29.  
  30. void fillArr(int* begin, int* end)
  31. {
  32.     while (begin != end)
  33.     {
  34.         *begin++ = rand()%10;
  35.     }
  36.     std::cout << '\n';
  37. }
  38.  
  39. int main()
  40. {
  41.     const int arrSize{ 10 };
  42.     int arr[arrSize]{ 1,2,3,4,5,6,7,8,9,10 };
  43.  
  44.     int* ptr{ arr + 4 }; // 100
  45.  
  46.     int* begin{ arr };
  47.     int* end{ arr + arrSize };
  48.  
  49.    
  50.     printArr(arr, 3, 8);
  51.     printArr(arr + 3, arr + 8);
  52.    
  53.     fillArr(arr + 3, arr + arrSize - 3);
  54.     printArr(arr, arr + arrSize);
  55.  
  56.  
  57.     int num{ 42 };
  58.     const int answer{ 42 };
  59.     const int badAnswer{ -15 };
  60.     int* ptrI{ &num };
  61.     const int* ptrIC{ &num };
  62.     int* const beginArr{ arr };
  63.     const int* const zhest{ arr };
  64.  
  65.     int* ptr;                   //simple pointer                pointer - rw    *pointer - rw
  66.     const int* ptrC;            //pointer to constant           pointer - rw    *pointer - ro
  67.     int* const cPtr{};          //constant pointer              pointer - ro    *pointer - rw
  68.     const int* const cPtrC{};   //constant pointer to constant  pointer - ro    *pointer - ro
  69.  
  70.     int* point{ nullptr };
  71.     point = &num; // single variable
  72.     point = arr; // array begin, multipe variables
  73.  
  74.  
  75.  
  76.  
  77.     std::cout << *ptrIC << '\n';
  78.     //*ptrI = 100500;
  79.     ptrIC = &num;
  80.     *ptrIC = 100500;
  81.     std::cout << *ptrIC << '\n';
  82.  
  83.     //int num{42}; //4
  84.     //int num1{ 33 };
  85.  
  86.     //float val{ 42 }; // 4
  87.  
  88.     //bool flag{}; // 1
  89.  
  90.     //uint16_t word{ 1005 }; // 2
  91.  
  92.     //double temp{ 36.675 };
  93.  
  94.     //std::cout << "num is living at " << *(&num) << " address\n";
  95.     //std::cout << "val is living at " << *(&val) << " address\n";
  96.     //std::cout << "flag is living at " << *(&flag) << " address\n";
  97.  
  98.     ////int address{ (int)&num };
  99.  
  100.     ////std::cout << " address = " << address << '\n';
  101.  
  102.  
  103.     ////void fun(int par);
  104.     ////void fun(int* par);
  105.  
  106.     ////fun(0);
  107.     ////fun(NULL);
  108.     ////fun(nullptr);
  109.     ////nullptr_t
  110.     //// nullptr
  111.     //int* ptrNum{nullptr}; //4
  112.     //bool isNull{ nullptr };
  113.  
  114.     //std::cout << "In ptrNum is address: "<< ptrNum << '\n';
  115.     //std::cout << std::boolalpha << isNull << '\n';
  116.     //
  117.     //{
  118.     //  int shortLifeVar{ 1111 };
  119.     //  ptrNum = &shortLifeVar;
  120.  
  121.     //  std::cout << "Inside block *ptrNum = " << *ptrNum << '\n';
  122.     //}
  123.  
  124.     //std::cout << "Outside block *ptrNum = " << *ptrNum << '\n';
  125.  
  126.     //if (ptrNum)
  127.     //{
  128.     //  std::cout << *ptrNum << '\n';
  129.     //}
  130.     //else
  131.     //{
  132.     //  std::cout << "nullptr detected! cannot dereference!!\n";
  133.     //}
  134.  
  135.    
  136.    
  137.     //[100][104][108][112][116][120][124][128][132][136]
  138.     // 5
  139.     //100 + 5 * sizeof(int)
  140.     //100 + 5 * 4
  141.     //100 + 20 -> 120
  142.  
  143.  
  144.     //ptr = ptr + 2; // 100 + 2 * sizeof(int) -> 100 + 2 * 4 -> 100 + 8 -> 108
  145.     //
  146.     //std::cout << *ptr << '\n';
  147.     //--ptr; // 108 - 1 * sizeof(int)  - > 108 - 4 -> 104
  148.     //std::cout << *ptr << '\n';
  149.  
  150.     //std::cout << *(arr + 1) << " < = > " << ptr[1] << '\n';
  151.    
  152.     // *(ptr + i) < = > ptr[i]
  153.  
  154.  
  155.     //for (int i{ 0 }; i < arrSize; ++i)
  156.     //{
  157.     //  //std::cout << "Address: " << ptr + i << " = " << *(ptr + i) << '\n';
  158.     //  std::cout << *arr << " < = > " << ptr[0] << '\n';
  159.     //}
  160.     // *(ptr + i) < - > arr[i] < - > ptr[i]
  161.  
  162.     /*std::cout << std::boolalpha << (&arr[0] == &arr[0]) << '\n';
  163.    
  164.     std::cout << std::boolalpha << (&arr[0] <= &arr[0]) << '\n';*/
  165.     //std::cout << arr << '\n';
  166.  
  167.  
  168.    
  169.  
  170.  
  171.     //while (begin != end)
  172.     //{
  173.     //  std::cout << *begin++ << ' ';
  174.     //}
  175.     //std::cout << '\n';
  176.  
  177.     //int size;
  178.  
  179.     //std::cout << "Enter array size:\n";
  180.     //std::cin >> size;
  181.     //int* ptr{ new int[size] {1,2,3,4,5,6,7,8,9,10,11,12} };
  182.     //begin = ptr;
  183.     //end = ptr + size;
  184.  
  185.     //while (begin != end)
  186.     //{
  187.     //  std::cout << *begin++ << ' ';
  188.     //}
  189.     //std::cout << '\n';
  190.  
  191.  
  192.     //delete[] ptr;
  193.  
  194.     return 0;
  195. }
RAW Paste Data