/* * pointref.cpp * * Created on: 03.12.2013 * Author: trifon */ #include #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 const MAX = 10; void printMatrix(int M[][MAX], int m, int n) { // void printMatrix(int M[43905][MAX], int m, int n) { // void printMatrix(int (*M)[MAX], int m, int n) { // M[] <-> (*M) for(int i = 0; i < m; i++) { // cout << M[i] << endl; for(int j = 0; j < n; j++) cout << M[i][j] << '\t'; cout << endl; } } int readStrings(char s[][MAX]) { int n; cout << "n = "; cin >> n; for(int i = 0; i < n; i++) { cout << "s[" << i << "] = "; cin >> s[i]; } return n; } void printStrings(char s[][MAX], int n) { for(int i = 0; i < n; i++) cout << s[i] << endl; } int* pointMax(int* p, int* q) { if (*p > *q) return p; return q; } int* pointMaxBad(int* p, int* q) { int max = *q; if (*p > max) return p; return &max; } char const* firstDifference(char const* s1, char const* s2) { while(*s1 != '\0' && *s2 != '\0' && *s1 == *s2) { // !!! *s1 = 'X'; s1++; s2++; } return s1; } 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; int* r = pointMaxBad(&x, &y); (*r)--; cout << x << ' ' << y << endl; int a[MAX][MAX] = { { 1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; printMatrix(a, 3, 3); char s[MAX][MAX] = { "abc", "def", "ghi" }; printStrings(s, 3); int n = readStrings(s); printStrings(s, n); char s2[] = "Hello, world!"; char s3[] = "Hellish world!"; if (strchr(s2, 'L')) cout << "Yes" << endl; else cout << "No" << endl; cout << strchr(s2, 'l') << endl; cout << firstDifference(s2, s3); cout << s2; return 0; }