Advertisement
avr39ripe

cppPointerReferenceDrill

May 5th, 2021
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.04 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int sum(int a, int b)
  4. {
  5.     //std::cout << "Sum of a nad b is: " << (a + b) << '\n';
  6.     return (a + b);
  7. }
  8.  
  9.  
  10.  
  11. void printArr(const int* begin,const int* const end)
  12. {
  13.     while(begin != end)
  14.     {
  15.         std::cout << *begin << ' ';
  16.         ++begin;
  17.     }
  18.     std::cout << '\n';
  19. }
  20.  
  21. void swap(int a, int b)
  22. {
  23.     int tmp{};
  24.    
  25.     std::cout << "inside swap Before: a = " << a << " b = " << b << '\n';
  26.  
  27.     tmp = a;
  28.     a = b;
  29.     b = tmp;
  30.  
  31.     std::cout << "inside swap After: a = " << a << " b = " << b << '\n';
  32. }
  33.  
  34. void swapP(int* const a, int* const b)
  35. {
  36.     int tmp{};
  37.  
  38.     std::cout << "inside swap Before: a = " << *a << " b = " << *b << '\n';
  39.  
  40.     tmp = *a;
  41.     *a = *b;
  42.     *b = tmp;
  43.  
  44.     std::cout << "inside swap After: a = " << *a << " b = " << *b << '\n';
  45. }
  46.  
  47. void swapR(int& a, int& b)
  48. {
  49.     int tmp{};
  50.  
  51.     std::cout << "inside swap Before: a = " << a << " b = " << b << '\n';
  52.  
  53.     tmp = a;
  54.     a = b;
  55.     b = tmp;
  56.  
  57.     std::cout << "inside swap After: a = " << a << " b = " << b << '\n';
  58. }
  59.  
  60. void multiRet(bool* const retB, float* const retF, char* const retC)
  61. {
  62.     bool resB;
  63.     float resF;
  64.     char resC;
  65.     //...
  66.  
  67.     //.. tupim :(
  68.     //retB++;
  69.     //retC = &resC;
  70.     //retF += 20;
  71.     // .. ne tupim :)
  72.  
  73.     resB = true;
  74.     resF = 36.6;
  75.     resC = 't';
  76.  
  77.     *retB = resB;
  78.     *retF = resF;
  79.     *retC = resC;
  80. }
  81.  
  82. int main()
  83. {
  84.     //int num{ 42 };//[num <->100 addr | new block of 4 bytes -> 42]
  85.     //int val{ 33 };//[val <->154 addr | new block of 4 bytes -> 33]
  86.     //int* ptrI{ &num };
  87.     //int* goodPointer{nullptr};
  88.  
  89.  
  90.     //// reference type
  91.     //int& refI{ num };//[refI <->num<->100 NO new block of 4 bytes allocated! -> 42]
  92.  
  93.     ////int* const refAsPtr{&num};
  94.     ////        we do not use pointer itself!
  95.     ////refAsPtr = &val;
  96.     ////std::cout << refAsPtr;
  97.     ////       we use only DEREFERENCED pointer so no need for asterics * :)
  98.     ////refAsPtr = 444;
  99.     ////std::cout << refAsPtr;
  100.  
  101.     //std::cout << "num =\t" << num << '\n' << "refI =\t" << refI << '\n';
  102.  
  103.     //refI = 321;
  104.  
  105.     //std::cout << "num =\t" << num << '\n' << "refI =\t" << refI << '\n';
  106.  
  107.     //return 0;
  108.  
  109.     //// simple pointer no const at all!!
  110.  
  111.     //std::cout << ptrI << '\n'; //GET ptrI value -> GET address
  112.     //ptrI = &val; // SET ptrI value -> SET address
  113.  
  114.     //std::cout << *ptrI << '\n'; //get ptrI address and GET value stored at this address -> GET value
  115.     //*ptrI = 555; // get ptrI address and SET value at this address -> SET value
  116.  
  117.     //// pointer to const
  118.  
  119.     //const int* ptrCI{ &num };
  120.     //std::cout << ptrCI << '\n'; //GET ptrI value -> GET address
  121.     //ptrCI = &val; // SET ptrI value -> SET address
  122.  
  123.     //std::cout << *ptrCI << '\n'; //get ptrI address and GET value stored at this address -> GET value
  124.     ////NOT POSSIBLE! CUT BY const int*
  125.     ////*ptrCI = 555; // get ptrI address and SET value at this address -> SET value
  126.  
  127.     //// const pointer
  128.  
  129.     //int* const ptrIC{ &num };
  130.     //std::cout << ptrIC << '\n'; //GET ptrI value -> GET address
  131.     //// NOT POSSIBLE CUT BY int* const
  132.     ////ptrIC = &val; // SET ptrI value -> SET address
  133.  
  134.     //std::cout << *ptrIC << '\n'; //get ptrI address and GET value stored at this address -> GET value
  135.     //*ptrIC = 555; // get ptrI address and SET value at this address -> SET value
  136.  
  137.  
  138.     //// hardcore const pointer to const
  139.  
  140.     //const int* const ptrZ{ &num };
  141.  
  142.     //std::cout << ptrZ << '\n'; //GET ptrI value -> GET address
  143.     ////// NOT POSSIBLE CUT BY int* const
  144.     ////ptrZ = &val; // SET ptrI value -> SET address
  145.  
  146.     //std::cout << *ptrZ << '\n'; //get ptrI address and GET value stored at this address -> GET value
  147.     //// NOT POSSIBLE CUT BY const int*
  148.     //// *ptrZ = 555; // get ptrI address and SET value at this address -> SET value
  149.  
  150.  
  151.     //bool flag{false};
  152.     //float temp{0};
  153.     //char desc{'z'};
  154.  
  155.     //std::cout << flag << ' ' << temp << ' ' << desc << '\n';
  156.  
  157.     //multiRet(&flag, &temp, &desc);
  158.  
  159.     //std::cout << flag << ' ' << temp << ' ' << desc << '\n';
  160.  
  161.     int valA{ 10 };
  162.     int valB{ 20 };
  163.  
  164.     std::cout << "inside main Before: valA = " << valA << " valB = " << valB << '\n';
  165.  
  166.     //swap(valA, valB);
  167.     //swapP(&valA, &valB);
  168.     swapR(valA, valB);
  169.  
  170.     std::cout << "inside main After: valA = " << valA << " valB = " << valB << '\n';
  171.  
  172.     //int val{ 33 };
  173.  
  174.     //const int arrSize{ 5 };
  175.     //int arr[arrSize]{ 1,2,3,4,5 }; // int* const arr
  176.  
  177.     //const int* const zhest{ &val };
  178.     //
  179.     //zhest = &val;
  180.  
  181.  
  182.     //*arr = 543;
  183.  
  184.  
  185.     //int num{ 42 };
  186.  
  187.     //int* const ptrArr{ arr };
  188.     //std::cout << (ptrArr == arr) << '\n';
  189.     //ptrArr = &num;
  190.     //++ptrArr;
  191.  
  192.     //arr = &num;
  193.     //++arr;
  194.  
  195.     //bool flag{ true };
  196.     //ptrArr = &flag; // (&flag) -> bool*
  197.  
  198.    
  199.    
  200.     const int arrSize{ 10 };
  201.     int arrA[arrSize]{1,2,3,4,5,6,7,8,9,10};
  202.     int arrB[arrSize]{2,4,6,8,10};
  203.  
  204.     //int* ptrA{ arrA };
  205.     //int* ptrB{ arrB };
  206.     //
  207.     //printArr(arr, arrSize, 0 , 5);
  208.     /*printArr(arrA + 4, arrA + 10);
  209.     printArr(arrA, arrA + 10);
  210.     printArr(arrA, arrA + 10);
  211.  
  212.     printArr(arrB, arrB + 5);*/
  213.     //int* begin{ arr };
  214.     //int* end{ arr + arrSize };
  215.  
  216.     //while(begin != end)
  217.     //{
  218.     //  std::cout << *begin++ << ' ';
  219.     //}
  220.     //std::cout << '\n';
  221.  
  222.     //for (int i{ 0 }; i < arrSize; ++i)
  223.     //{
  224.     //  std::cout << *ptr++ << ' ';
  225.     //}
  226.     //std::cout << '\n';
  227.        
  228.     return 0;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement