/* Pointers */ #include #include using namespace std; /* Definition, declaration, dereferencing Pointers are special variables that holds a memory address, location e.g. */ /* int main() { // declaration * int *i; // OR int* i, int * i; char *c; // assignment -> & int v = 5; int *j; // create a integer pointer j j = &v; //assign the address of v to it. // OR // int v = 5; // int *j = &v; // print the j and the value j holds ( dereference using * ) cout << "j=" << j << "\t value of j (v's address)=" << *j << endl; // j's value is printed as hexadecimal in previous statement. // print the same statement using c-style I/O w/ flag %p printf("j=%p \t value of j (v's address)= %d\n", j, *j); int *k = 0; // NULL pointer cout << k << endl; return 0; } */ /* Array names are pointers. They point to the first element in the array Example: Write a program that declares an array of size 5 and initializes it to numbers from 0 to 4 using pointers. */ /* int main() { int i, *j, arr[5]; for(int i = 0; i != 5; i++) arr[i] = i; cout << "arr=" << arr << endl; for(int i=0; i!=5; i++){ cout << "i = " << i; cout << " arr[i] = " << arr[i]; cout << " &arr[i] = " << &arr[i]; cout << endl; } cout << endl; j = &arr[0]; cout << "j=" << j; cout << " *j=" << *j; cout << endl << endl; j++; cout << "after adding 1 to j: j = " << j; cout << " *j=" << *j << endl; } */ /* Since pointers hold memory address(int) they are represented as integers themselves ex: */ /* int main() { int x, y; int *px; x = 3; px = &x; y = *px; printf("x=%d, px=%p, y=%d \n", x, px, y); x++ ; printf("x=%d, px=%p, y=%d \n", x, px, y); (*px)++; printf("x=%d, px=%p, y=%d \n", x, px, y); *px++; printf("x=%d, px=%p, y=%d \n", x, px, y); printf("*px=%d\n", *px); return 0; } */ /* References ( aliases ), & unary operator */ /* int main(){ int i = 5; int &ii = i; // references are like constants, they have to be initialized when declared. ERROR: // int ⅈ // ii = i; double arr[10]; double &last = arr[9]; return 0; } */ /* Usage of * and & operators (Summary): | declaration | statement ---------------------------------------------- * | pointer | returns the value of a ptr ---------------------------------------------- & | reference | returns the address of a var ---------------------------------------------- Example: */ /* int main() { int B = 2; int A = 1; int *p = &A; int &refA = A; *p = 3; cout << "A= " << A << " p=" << p << " *p=" << *p << " refA=" << refA << " &A=" << &A << " &p=" << &p << " &refA=" << &refA << endl; A = *p + 1; cout << "A= " << A << " p=" << p << " *p=" << *p << " refA=" << refA << " &A=" << &A << " &p=" << &p << " &refA=" << &refA << endl; A = refA + 1; cout << "A= " << A << " p=" << p << " *p=" << *p << " refA=" << refA << " &A=" << &A << " &p=" << &p << " &refA=" << &refA << endl; A++; cout << "A= " << A << " p=" << p << " *p=" << *p << " refA=" << refA << " &A=" << &A << " &p=" << &p << " &refA=" << &refA << endl; p++; cout << "A= " << A << " p=" << p << " *p=" << *p << " refA=" << refA << " &A=" << &A << " &p=" << &p << " &refA=" << &refA << endl; refA++; cout << "A= " << A << " p=" << p << " *p=" << *p << " refA=" << refA << " &A=" << &A << " &p=" << &p << " &refA=" << &refA << endl; return 0; } */ /* Generic pointers - are pointers without a type. They are declared void and they can't be dereferenced without a cast */ /* int main(){ int *pi; char *pc; void *pv; // generic pointer int val = 4; char c = 'a'; pi = &val; pc = &c; cout << "*pi: " << *pi << endl; cout << "*pc: " << *pc << endl; pv = pc; pv = pi; //cout << "*pv=" << *pv << endl; // illegal pi = reinterpret_cast(pv); cout << "*pi(retreived from pv): " << *pi << endl; return 0; } */ /* Dynamic memory allocation - You can allocate memory during runtime by using keyword new and delete. e.g. int *p, *q, *r; p = new int(5); // generate a new integer and initialize it to 5 and return its address to p. q = new int[10]; // allocate space for an int array of size 10 and retunr the first elements // address to q r = new int; // generate a new integer and return its address to r. dont' initialize. delete p ; // free up the memory that p points to delete[] q; // free up the memory spaces where q points to the first element Dynamic array declaration: */ #include // DON'T comment this line /* int main(){ int *data; int size; cout << "Enter array size: " ; cin >> size; assert(size > 0); data = new int[size]; assert(data != 0); for(int i=0; i !=size; i++){ cout << (data[i] = i) << '\t'; } cout << endl; delete[] data; return 0; } */ /* Function calls ( call-by-value vs. call-by-reference ) call-by-value example: */ /* void myfun( int a ) { a++; cout << "inside myfun a=" << a << endl; } int main(){ int x = 7; myfun(x); cout << "after myfun x=" << x << endl; } */ /* call-by-reference example 1 using pointers: */ /* void myfun( int *a ) { (*a)++; cout << "inside myfun a=" << *a << endl; } int main(){ int x = 7; myfun(&x); cout << "after myfun x=" << x << endl; } */ /* call-by-reference example 2 using references: */ /* void myfun( int &a ) { a++; cout << "inside myfun a=" << a << endl; } int main(){ int x = 7; myfun(x); cout << "after myfun x=" << x << endl; } */ /* Another example: */ /* void myfun( int a, int *b, int &c ) { a++; (*b)--; c = a + (*b); cout << "a=" << a << " b=" << b << " c=" << c << endl; } int main() { int x = 7 ; myfun( x, &x, x ); cout << "x=" << x << endl; } */ /* Passing arrays to a function Arrays are always passed by-reference. Ex: */ /* void addone( int A[] ) { cout << "in addone, after adding one, A:"; for(int i=0; i!=3; i++){ A[i]++; cout << '\t' << A[i]; } cout << endl << endl; } int main() { int A[3] = {1,2,3}; cout << "in main, before addone A:"; for(int i=0; i!=3; i++) cout << '\t' << A[i]; cout << endl << endl; addone(A); cout << "in main, after addone A:"; for(int i=0; i!=3; i++) cout << '\t' << A[i]; cout << endl << endl; } */ /* Pointers to Objects: It is also possible to create pointers to user defined types such as class and struct objects. Ex: */ class Point { public: Point(){} Point(int x0, int y0): x(x0), y(y0) {} void set(int x0, int y0) { x = x0; y = y0; } int getX() { return x; } int getY() { return y; } void print() const { cout << "(" << x << "," << y << ") ";} private: int x, y; }; int main(){ Point *triagain = new Point[3]; assert( triagain != 0 ); triagain->set(0,0); // using -> is equivalent with (*triagain).set(0,0); triagain++; triagain->set(0,3); triagain++; triagain->set(3,0); triagain--; triagain--; cout << "tri-ing again: "; for(int i=0; i!=3; i++) triagain[i].print(); cout << endl; delete[] triagain; }