/* * pointref.cpp * * Created on: 29.11.2012 * Author: trifon */ #include #include using namespace std; int main1() { int x = 5, *p = &x, *q = p, y = *p + 2; cout << p << ' ' << q << endl; cout << *p << ' ' << *q << endl; cout << &p << ' ' << &q << endl; *p = 8; cout << *p << ' ' << *q << ' '; cout << x << ' ' << y << endl; (*p)++; // не е същото като! *(p++) <--> *p++ cout << *p << endl; p = &y; cout << *p << ' ' << *q << ' '; cout << x << ' ' << y << endl; *q = 1; cout << *p << ' ' << *q << ' '; cout << x << ' ' << y << endl; *p = *q; // y = x cout << *p << ' ' << *q << ' '; cout << x << ' ' << y << endl; double z = 1.23, *r = &z; cout << p << ' ' << p + 2 << endl; cout << r << ' ' << r + 2 << endl; void* s; // void* <- int* s = p; s = q; s = &x; // void* <- double* s = &z; s = r; // void* <- ?* s = &r; // void* <- double** s = &s; // void* <- void** s = p; cout << *(int*)s << endl; s = &z; cout << *(double*)s << endl; cout << *(int*)s << endl; double* t = (double*)s; cout << *t << endl; cout << s << ' ' << s + 2 << endl; cout << *(double*)p << endl; const int a = 8; cout << &a << endl; cout << *(&a) << endl; // !!! *(&a) = 5; //!!! p = &a; *p = 2; int * const cp = p; cout << *cp << endl; *cp = 10; cout << *cp << endl; /// !!!!! cp = q; int const* pc = &x; x = 20; // !!! *pc = 20; *q = 20; pc = p; // !!! *pc = 40; pc = &a; // !!! p = pc; p = (int*)pc; *p = 10; cout << a << endl; int const* const cpc = &a; //!!! *cpc = 5; //!!! cpc = pc; int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; //arr = &x; //arr++; //arr--; //arr = arr + 2; cout << *arr << endl; cout << *(arr+4) << endl; *(arr+6) = 60; cout << arr[6] << endl; cout << 6[arr] << endl; p = arr; //x = a; while (*p < 10) { cout << *p++ << ','; } cout << *p << endl; int b[2][2][3] = { { {1,2,3}, {4,5,6} }, { {7,8,9}, {10,11,12} } }; cout << b << endl; cout << b[0] << ' ' << b[1] << endl; cout << b[0][0] << ' ' << b[0][1] << endl; cout << b[1][0] << ' ' << b[1][1] << endl; cout << b + 1 << ' ' << *(b + 1) << endl; for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) for(int k = 0; k < 3; k++) //cout << *(*(*(b + i)+j)+k) << endl; cout << b[i][j][k] << endl; char str[] = "Hello, world!"; char* ps = str; while (*ps) cout << *ps++ << ' '; cout << endl; do { cout << *--ps << ' '; } while (ps != str); // ps == str cout << endl; cout << (void*)ps << ' ' << (void*)str << endl; while (*ps) { cout << *ps << ' '; ps += 2; } cout << "abc" << endl; char const* strc = "Hello, world!"; cout << strc; char* strp = "Hi C++!"; cout << strp; strp[1] = 'o'; return 0; } int main() { int x = 3; int &a = x, b = a; int &c = b; a = c + 5; b = 2; x = 10; int &d = c; d = 5; return 0; cout << a << ' ' << x << endl; cout << b << ' ' << c << ' ' << d << endl; } int main2() { int x = 3; int * const pa = &x, b = *pa; int * const pc = &b; // !!!!! pa = pc + 5; *pa = *pc + 5; b = 2; x = 10; // !!! pa = &b; // !!! pc = &x; cout << *pa << ' ' << x << endl; cout << b << ' ' << *pc << endl; return 0; } int f(); int f(); int f(); int f(); int f(); int f(); int g() { return f(); } int f() { return g(); } int test(); int test2() { //!!! return test(); }