Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int sum(int a, int b)
- {
- //std::cout << "Sum of a nad b is: " << (a + b) << '\n';
- return (a + b);
- }
- void printArr(const int* begin,const int* const end)
- {
- while(begin != end)
- {
- std::cout << *begin << ' ';
- ++begin;
- }
- std::cout << '\n';
- }
- void swap(int a, int b)
- {
- int tmp{};
- std::cout << "inside swap Before: a = " << a << " b = " << b << '\n';
- tmp = a;
- a = b;
- b = tmp;
- std::cout << "inside swap After: a = " << a << " b = " << b << '\n';
- }
- void swapP(int* const a, int* const b)
- {
- int tmp{};
- std::cout << "inside swap Before: a = " << *a << " b = " << *b << '\n';
- tmp = *a;
- *a = *b;
- *b = tmp;
- std::cout << "inside swap After: a = " << *a << " b = " << *b << '\n';
- }
- void swapR(int& a, int& b)
- {
- int tmp{};
- std::cout << "inside swap Before: a = " << a << " b = " << b << '\n';
- tmp = a;
- a = b;
- b = tmp;
- std::cout << "inside swap After: a = " << a << " b = " << b << '\n';
- }
- void multiRet(bool* const retB, float* const retF, char* const retC)
- {
- bool resB;
- float resF;
- char resC;
- //...
- //.. tupim :(
- //retB++;
- //retC = &resC;
- //retF += 20;
- // .. ne tupim :)
- resB = true;
- resF = 36.6;
- resC = 't';
- *retB = resB;
- *retF = resF;
- *retC = resC;
- }
- int main()
- {
- //int num{ 42 };//[num <->100 addr | new block of 4 bytes -> 42]
- //int val{ 33 };//[val <->154 addr | new block of 4 bytes -> 33]
- //int* ptrI{ &num };
- //int* goodPointer{nullptr};
- //// reference type
- //int& refI{ num };//[refI <->num<->100 NO new block of 4 bytes allocated! -> 42]
- ////int* const refAsPtr{&num};
- //// we do not use pointer itself!
- ////refAsPtr = &val;
- ////std::cout << refAsPtr;
- //// we use only DEREFERENCED pointer so no need for asterics * :)
- ////refAsPtr = 444;
- ////std::cout << refAsPtr;
- //std::cout << "num =\t" << num << '\n' << "refI =\t" << refI << '\n';
- //refI = 321;
- //std::cout << "num =\t" << num << '\n' << "refI =\t" << refI << '\n';
- //return 0;
- //// simple pointer no const at all!!
- //std::cout << ptrI << '\n'; //GET ptrI value -> GET address
- //ptrI = &val; // SET ptrI value -> SET address
- //std::cout << *ptrI << '\n'; //get ptrI address and GET value stored at this address -> GET value
- //*ptrI = 555; // get ptrI address and SET value at this address -> SET value
- //// pointer to const
- //const int* ptrCI{ &num };
- //std::cout << ptrCI << '\n'; //GET ptrI value -> GET address
- //ptrCI = &val; // SET ptrI value -> SET address
- //std::cout << *ptrCI << '\n'; //get ptrI address and GET value stored at this address -> GET value
- ////NOT POSSIBLE! CUT BY const int*
- ////*ptrCI = 555; // get ptrI address and SET value at this address -> SET value
- //// const pointer
- //int* const ptrIC{ &num };
- //std::cout << ptrIC << '\n'; //GET ptrI value -> GET address
- //// NOT POSSIBLE CUT BY int* const
- ////ptrIC = &val; // SET ptrI value -> SET address
- //std::cout << *ptrIC << '\n'; //get ptrI address and GET value stored at this address -> GET value
- //*ptrIC = 555; // get ptrI address and SET value at this address -> SET value
- //// hardcore const pointer to const
- //const int* const ptrZ{ &num };
- //std::cout << ptrZ << '\n'; //GET ptrI value -> GET address
- ////// NOT POSSIBLE CUT BY int* const
- ////ptrZ = &val; // SET ptrI value -> SET address
- //std::cout << *ptrZ << '\n'; //get ptrI address and GET value stored at this address -> GET value
- //// NOT POSSIBLE CUT BY const int*
- //// *ptrZ = 555; // get ptrI address and SET value at this address -> SET value
- //bool flag{false};
- //float temp{0};
- //char desc{'z'};
- //std::cout << flag << ' ' << temp << ' ' << desc << '\n';
- //multiRet(&flag, &temp, &desc);
- //std::cout << flag << ' ' << temp << ' ' << desc << '\n';
- int valA{ 10 };
- int valB{ 20 };
- std::cout << "inside main Before: valA = " << valA << " valB = " << valB << '\n';
- //swap(valA, valB);
- //swapP(&valA, &valB);
- swapR(valA, valB);
- std::cout << "inside main After: valA = " << valA << " valB = " << valB << '\n';
- //int val{ 33 };
- //const int arrSize{ 5 };
- //int arr[arrSize]{ 1,2,3,4,5 }; // int* const arr
- //const int* const zhest{ &val };
- //
- //zhest = &val;
- //*arr = 543;
- //int num{ 42 };
- //int* const ptrArr{ arr };
- //std::cout << (ptrArr == arr) << '\n';
- //ptrArr = #
- //++ptrArr;
- //arr = #
- //++arr;
- //bool flag{ true };
- //ptrArr = &flag; // (&flag) -> bool*
- const int arrSize{ 10 };
- int arrA[arrSize]{1,2,3,4,5,6,7,8,9,10};
- int arrB[arrSize]{2,4,6,8,10};
- //int* ptrA{ arrA };
- //int* ptrB{ arrB };
- //
- //printArr(arr, arrSize, 0 , 5);
- /*printArr(arrA + 4, arrA + 10);
- printArr(arrA, arrA + 10);
- printArr(arrA, arrA + 10);
- printArr(arrB, arrB + 5);*/
- //int* begin{ arr };
- //int* end{ arr + arrSize };
- //while(begin != end)
- //{
- // std::cout << *begin++ << ' ';
- //}
- //std::cout << '\n';
- //for (int i{ 0 }; i < arrSize; ++i)
- //{
- // std::cout << *ptr++ << ' ';
- //}
- //std::cout << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement