/* * pointref.cpp * * Created on: 03.12.2013 * Author: trifon */ #include using namespace std; void test_string() { char s[] = "Hello, world!"; char* p = s; while (*p) { cout << *p << ' '; p++; } do { p--; cout << *p; } while (p != s); // p == s } void test_pointers() { int *pi; double *pd = NULL; double d = 1.23; double *qd = &d; // !!! double *qd2 = &qd; double **qqd = &qd; // !!! double **qqd2 = &d; // !!!! pi = (int*)π cout << pi << ' ' << pd << ' ' << d << ' ' << qd << ' ' << qqd << endl; int x = 5, *p = &x, *q = p, y = *p + 2; // !!! cout << *p++ << endl; cout << (*p)++ << endl; cout << x << ' ' << y << ' ' << &x << ' ' << &y << endl; // !!! cout << *x << ' ' << *y << endl; cout << p << ' ' << q << ' ' << *p << ' ' << *q << endl; cout << &p << ' ' << &q << endl; } void print_array(int a[], int n) { for(int i = 0; i < n; i++) cout << a[i] << ' '; cout << endl; } void test_sizes() { cout << "char: " << sizeof(char) << endl; cout << "int: " << sizeof(int) << endl; cout << "long: " << sizeof(long) << endl; cout << "float: " << sizeof(float) << endl; cout << "double: " << sizeof(double) << endl; int a[5] = { 1, 2, 3, 4, 5 }; int* p = a; char s[] = "Hello, world!"; char* q = s; cout << "a: " << sizeof(a) << endl; cout << "p: " << sizeof(p) << endl; cout << "s: " << sizeof(s) << endl; cout << "q: " << sizeof(q) << endl; cout << p << ' ' << (p + 5) << endl; cout << (long)q << ' ' << (long)(q + 5) << endl; cout << 2[a] << ' ' << a[2] << endl; cout << s << ' ' << s + 2 << endl; print_array(a, 5); print_array(a + 2, 3); int const* r = a; r++; cout << *r << endl; // !!! *r = 15; // !!! int* r2 = r; // !!! *r2 = 15; int *r2 = (int*)r; *r2 = 15; print_array(a, 5); const int N = 5; // !!! p = &N; // !!! *p = 30; r = &N; cout << *r << endl; char *s2 = "Hi C++!"; // указател, който сочи към някаква ??? памет, съдържаща низа char s3[] = "Hi C++!"; // масив, който съдържа низа // !!! s2[1] = 'o'; int M[N][N]; int element = 0; for(int i = 0; i < N; i++) for(int j = 0; j < N; j++ ) M[i][j] = element++; for(int i = 0; i < N; i++) print_array(M[i]+i, N-i); } void test_voidstar() { int i = 10; double d = 1.23; char c = 'X'; bool b = true; void* p; p = &i; p = &d; p = &c; p = &b; p = &p; p = &d; // !!! p = &c; // !!! cout << *p << endl; // да приемем, че знаем отнякъде, че p сочи към bool // bool* pb = (bool*)p; // cout << *pb << endl; // да приемем, че знаем отнякъде, че p сочи към double double* pd = (double*)p; cout << *pd << endl; } void test_reference() { int x = 5; int& y = x; cout << x << ' ' << y << endl; y = 6; cout << x << ' ' << y << endl; x = 7; cout << x << ' ' << y << endl; // !!! int& z; // !!! int * const p; // !!! int const N; int z, &t = z, &u = x; int const* p = &x; // !!! *p = 10; // !!! int& a = *p; // !!! a = 10; int const& b = *p; int *const px = &x; *px = 10; y = 10; } void f(int x) { x++; cout << "f: " << x << endl; } void g(int* p) { (*p)++; cout << "g: " << *p << endl; } void swap(int* p, int* q) { int tmp = *p; *p = *q; *q = tmp; } int main() { // test_pointers(); // test_string(); // test_sizes(); // test_voidstar(); // test_reference(); int x = 20, y = 10; f(y); cout << y << endl; g(&y); cout << y << endl; swap(&x, &y); cout << x << ' ' << y << endl; return 0; }