avr39ripe

cppPointersBasics

Aug 16th, 2021
1,217
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3.  
  4.  
  5. int main()
  6. {
  7.     int num{ 42 }; //4
  8.     int num1{ 33 };
  9.  
  10.     float val{ 42 }; // 4
  11.  
  12.     bool flag{}; // 1
  13.  
  14.     uint16_t word{ 1005 }; // 2
  15.  
  16.     double temp{ 36.675 };
  17.  
  18.     std::cout << "num is living at " << &num << " address\n";
  19.     std::cout << "val is living at " << &val << " address\n";
  20.     std::cout << "flag is living at " << &flag << " address\n";
  21.  
  22.     int address{ (int)&num };
  23.  
  24.     std::cout << " address = " << address << '\n';
  25.  
  26.  
  27.     int* ptrNum{ &num }; //4
  28.     float* ptrVal{ &val }; //4
  29.     bool* ptrFlag{&flag}; //4
  30.     uint16_t* ptrWord{ &word }; //4
  31.     double* ptrTemp{ &temp };
  32.  
  33.     std::cout << "Size of ptrNum variable is " << sizeof(ptrNum) << '\n';
  34.     std::cout << "Size of ptrVal variable is " << sizeof(ptrVal) << '\n';
  35.     std::cout << "Size of ptrFlag variable is " << sizeof(ptrFlag) << '\n';
  36.     std::cout << "Size of ptrWord variable is " << sizeof(ptrWord) << '\n';
  37.     std::cout << "Size of ptrTemp variable is " << sizeof(ptrTemp) << '\n';
  38.  
  39.  
  40.     std::cout << "ptrNum = " << ptrNum << '\n';
  41.     std::cout << "ptrVal = " << ptrVal << '\n';
  42.     std::cout << "ptrFlag = " << ptrFlag << '\n';
  43.  
  44.     //ptrNum = &num1;
  45.  
  46.     std::cout << "*ptrNum = " << *ptrNum << '\n';
  47.     std::cout << "*ptrVal = " << *ptrVal << '\n';
  48.     std::cout << "*ptrFlag = " << *ptrFlag << '\n';
  49.  
  50.     *ptrNum = 76;
  51.     num = 333;
  52.     std::cout << num << '\n';
  53.  
  54.  
  55.     *ptrVal = 76;
  56.     *ptrFlag = true;
  57.  
  58.     std::cout << "*ptrNum = " << *ptrNum << '\n';
  59.     std::cout << "*ptrVal = " << *ptrVal << '\n';
  60.     std::cout << "*ptrFlag = " << *ptrFlag << '\n';
  61.  
  62.     std::cout << "num = " << num << '\n';
  63.     std::cout << "val = " << val << '\n';
  64.     std::cout << "flag = " << flag << '\n';
  65.  
  66.  
  67.     ptrNum = (int*)&val;
  68.     ptrVal = (float*)&num;
  69.  
  70.     std::cout << "*ptrNum = " << *ptrNum << '\n';
  71.     std::cout << "*ptrVal = " << *ptrVal << '\n';
  72.  
  73.     const int arrSize{ 10 };
  74.     int arr[arrSize]{ 1,2,3,4,5,6,7,8,9,10 };
  75.     int* begin{ arr };
  76.     int* end{ arr + arrSize };
  77.  
  78.  
  79.     while (begin != end)
  80.     {
  81.         std::cout << *begin++ << ' ';
  82.     }
  83.     std::cout << '\n';
  84.  
  85.     int size;
  86.  
  87.     std::cout << "Enter array size:\n";
  88.     std::cin >> size;
  89.     int* ptr{ new int[size] {1,2,3,4,5,6,7,8,9,10,11,12} };
  90.     begin = ptr;
  91.     end = ptr + size;
  92.  
  93.     while (begin != end)
  94.     {
  95.         std::cout << *begin++ << ' ';
  96.     }
  97.     std::cout << '\n';
  98.  
  99.  
  100.     delete[] ptr;
  101.  
  102.     return 0;
  103. }
RAW Paste Data