Advertisement
enkov

Пример за работа с указатели в С++

Jan 25th, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.  
  8.     // глава 9.1 - 9.6
  9.  
  10.     int i = 5;
  11.     int* ip = &i;
  12.     int arr[5] = { 1,2,3,4,5 };
  13.  
  14.     cout << "i = " << i << endl;
  15.  
  16.     cout << "&i = " << &i << endl;
  17.  
  18.     cout << "ip = " << ip << endl;
  19.  
  20.     cout << "&ip = " << &ip << endl;
  21.  
  22.     cout << "*ip = " << *ip << endl;
  23.  
  24.     cout << "&arr = " << &arr << endl;
  25.     cout << "&arr[0] = " << &arr[0] << endl;
  26.     cout << "&arr[1] = " << &arr[1] << endl;
  27.     cout << "&arr[2] = " << &arr[2] << endl;
  28.     cout << "&arr[3] = " << &arr[3] << endl;
  29.     cout << "&arr[4] = " << &arr[4] << endl;
  30.  
  31.     cout << arr[0] << endl;
  32.     ip = &arr[0];
  33.     cout << *ip << endl;
  34.     cout << "ip = " << ip << endl;
  35.     ip = ip + 4;
  36.     cout << "ip + 4 = " << ip << endl;
  37.     cout << *ip << endl;
  38.     cout << arr[4] << endl;
  39.  
  40.     // sizeof 9.2
  41.     cout << "size of char:" << sizeof(char) << endl;
  42.     cout << "size of int: " << sizeof(int) << endl;
  43.     cout << "size of double: " << sizeof(double) << endl;
  44.     cout << "size of 'A' :" << sizeof('A') << endl;
  45.     cout << "size of (3+20): " << sizeof(3 + 20) << endl;
  46.     cout << "size of (3*2.5): " << sizeof(3 * 2.5) << endl;
  47.     int* sp;
  48.     cout << "size of sp: " << sizeof sp << endl;
  49.     cout << "size of int* :" << sizeof(int*) << endl;
  50.  
  51.     // 9.3. Адресна аритметика
  52.     int mas[] = { 5,10,15,20,25,30,35,40,45,50 }; //3
  53.     int *p1, *p2; //4
  54.     p1 = &mas[3]; //5
  55.     p2 = &mas[7];
  56.     cout << "p1= " << p1 << '\t'; //7
  57.     cout << "*p1= " << *p1 << endl; //8
  58.     cout << "p2= " << p2 << '\t'; //9
  59.     cout << "*p2= " << *p2 << endl; //10
  60.     cout << "*(p1+1)= " << *(p1 + 1) << endl; //11
  61.     cout << "*(p1-1)= " << *(p1 - 1) << endl; //12
  62.     cout << "(p2-p1)= " << p2 - p1 << endl; //13
  63.     p2++; //14
  64.     cout << "*p2= " << *p2 << endl; //15
  65.     p1 -= 2; //16
  66.     cout << "*p1= " << *p1 << endl; //17
  67.    
  68.  
  69.     // 9.4. Указатели към void
  70.     { // правя фиктивен блок, за да мога да предефинирам
  71.     int i = 25;
  72.     double d = 2.5;
  73.     void* p;
  74.     p = &i; // p сочи данни от тип int
  75.     // cout << *p; // грешно!
  76.     cout << "p as int:" << *static_cast<int*>(p) << endl;
  77.     p = &d; // p сочи данни от тип double
  78.     cout << "p as double:" << *static_cast<double*>(p) << endl;
  79.     }
  80.     // динамична памет глава 9.7
  81.  
  82.     int* p = new int[10];
  83.     *p = 11;
  84.     cout << "p = " << p << endl;
  85.     cout << "*p = " << *p << endl;
  86.     cout << "p[0] = " << p[0] << endl;
  87.  
  88.     int n;
  89.     cout << "n= "; cin >> n;
  90.     double* dp = new double[n];
  91.     cout << "dynamic array address after new " << dp << endl;
  92.     for (int i = 0; i < n; i++)
  93.         dp[i] = i;
  94.     for (int i = 0; i < n; i++)
  95.         cout << "dp[" << i << "] = " << dp[i] << endl;
  96.     delete []dp;
  97.     cout << "dynamic array address after delete " << dp << endl;
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement