Tranvick

Example2

Jan 7th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. //Пример2. Указатели и массивы:
  2. //объявление массива и использование указателя; управление динамическим массивом; передача массива как параметра
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. void print(int * a, int n) {
  8.     for (int i = 0; i < n; ++i) cout << a[i] << " ";
  9.     cout << endl;
  10. }
  11.  
  12. int main() {
  13.     int a[5] = {1, 2, 3};
  14.     print(a, 5);
  15.    
  16.     cout << *(a + 2) << endl;
  17.  
  18.     int * p = a;
  19.     for (int i = 0; i < 5; ++i, ++p) cout << * p << " ";
  20.     cout << endl;
  21.  
  22.     p = new int [3];
  23.     p[0] = p[1] = p[2] = 10;
  24.     print(p, 3);
  25.     delete [] p;
  26.  
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment