Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- // глава 9.1 - 9.6
- int i = 5;
- int* ip = &i;
- int arr[5] = { 1,2,3,4,5 };
- cout << "i = " << i << endl;
- cout << "&i = " << &i << endl;
- cout << "ip = " << ip << endl;
- cout << "&ip = " << &ip << endl;
- cout << "*ip = " << *ip << endl;
- cout << "&arr = " << &arr << endl;
- cout << "&arr[0] = " << &arr[0] << endl;
- cout << "&arr[1] = " << &arr[1] << endl;
- cout << "&arr[2] = " << &arr[2] << endl;
- cout << "&arr[3] = " << &arr[3] << endl;
- cout << "&arr[4] = " << &arr[4] << endl;
- cout << arr[0] << endl;
- ip = &arr[0];
- cout << *ip << endl;
- cout << "ip = " << ip << endl;
- ip = ip + 4;
- cout << "ip + 4 = " << ip << endl;
- cout << *ip << endl;
- cout << arr[4] << endl;
- // sizeof 9.2
- cout << "size of char:" << sizeof(char) << endl;
- cout << "size of int: " << sizeof(int) << endl;
- cout << "size of double: " << sizeof(double) << endl;
- cout << "size of 'A' :" << sizeof('A') << endl;
- cout << "size of (3+20): " << sizeof(3 + 20) << endl;
- cout << "size of (3*2.5): " << sizeof(3 * 2.5) << endl;
- int* sp;
- cout << "size of sp: " << sizeof sp << endl;
- cout << "size of int* :" << sizeof(int*) << endl;
- // 9.3. Адресна аритметика
- int mas[] = { 5,10,15,20,25,30,35,40,45,50 }; //3
- int *p1, *p2; //4
- p1 = &mas[3]; //5
- p2 = &mas[7];
- cout << "p1= " << p1 << '\t'; //7
- cout << "*p1= " << *p1 << endl; //8
- cout << "p2= " << p2 << '\t'; //9
- cout << "*p2= " << *p2 << endl; //10
- cout << "*(p1+1)= " << *(p1 + 1) << endl; //11
- cout << "*(p1-1)= " << *(p1 - 1) << endl; //12
- cout << "(p2-p1)= " << p2 - p1 << endl; //13
- p2++; //14
- cout << "*p2= " << *p2 << endl; //15
- p1 -= 2; //16
- cout << "*p1= " << *p1 << endl; //17
- // 9.4. Указатели към void
- { // правя фиктивен блок, за да мога да предефинирам
- int i = 25;
- double d = 2.5;
- void* p;
- p = &i; // p сочи данни от тип int
- // cout << *p; // грешно!
- cout << "p as int:" << *static_cast<int*>(p) << endl;
- p = &d; // p сочи данни от тип double
- cout << "p as double:" << *static_cast<double*>(p) << endl;
- }
- // динамична памет глава 9.7
- int* p = new int[10];
- *p = 11;
- cout << "p = " << p << endl;
- cout << "*p = " << *p << endl;
- cout << "p[0] = " << p[0] << endl;
- int n;
- cout << "n= "; cin >> n;
- double* dp = new double[n];
- cout << "dynamic array address after new " << dp << endl;
- for (int i = 0; i < n; i++)
- dp[i] = i;
- for (int i = 0; i < n; i++)
- cout << "dp[" << i << "] = " << dp[i] << endl;
- delete []dp;
- cout << "dynamic array address after delete " << dp << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement