/* This program demonstrates what happens with pointers. Here is output from a sample run: myint is 3, intPtr is 0 myint is 3, intPtr is ffbfed60, intPtr points to 3 myint is 6, intPtr is ffbfed60, intPtr points to 6 Enter a number in the pointer function-> 246 myint is 246, intPtr is ffbfed60, intPtr points to 246 Enter a number in the pointer function-> 76 myint is 76, intPtr is ffbfed60, intPtr points to 76 x is 3; xPtr is 3; y is 10; yPtr is ffbfed58 x is 3; xPtr is a; y is 10; yPtr is ffbfed58 x is 10; xPtr is a; y is 10; yPtr is ffbfed58 a[0] is 2, *aPtr is 2 a[0] is 2, *aPtr is 6 *(aPtr+5) is 12 bptr is 6 */ int main() { int read(int *); // declare functions. in C++, necessary int *intPtr, myint; // only intPtr is ptr. int *intPtr, *myint // make both pointers int x = 3; /* for use below, */ int y = 10; /* past read function */ int *xPtr; /* but all variables must be declared at top of function */ int *yPtr = &y; // is same as type int *, then the assignment int a[10] = {2,4,6,8,10,12,14,16,18,20} ; /* for array manip section */ int* aPtr = a; int *bPtr; /* below array manip section */ myint = 3; intPtr = 0; // points to null // x to print in hex printf("myint is %d, intPtr is %x\n", myint, intPtr); /* print intPtr points to null, *intPtr would be runtime error line below creates an alias */ intPtr = &myint; printf("myint is %d, intPtr is %x, intPtr points to %d\n", myint, intPtr, *intPtr); *intPtr = 6; // changes myint without any reference to it. printf("myint is %d, intPtr is %x, intPtr points to %d\n", myint, intPtr, *intPtr); // passing parameters by pointer. similar to passing objects in java read(&myint); // calls pointer function printf("myint is %d, intPtr is %x, intPtr points to %d\n", myint, intPtr, *intPtr); read(intPtr); // calls pointer function printf("myint is %d, intPtr is %x, intPtr points to %d\n", myint, intPtr, *intPtr); // doing * and no * together xPtr = &x; // int *yPtr = &y; // is same as type int *, then the assignment printf("x is %d; xPtr is %x; y is %d; yPtr is %x\n", *xPtr, xPtr, *yPtr, yPtr); xPtr = yPtr; // changes xPtr to point to same place as y. x is unchanged printf("x is %d; xPtr is %x; y is %d; yPtr is %x\n", *xPtr, xPtr, *yPtr, yPtr); x = 3; y = 10; xPtr = &x; yPtr = &y; *xPtr = *yPtr; // * on LHS here means put in where xPtr points // changes value of where x points (now 10), but xPtr still // points to same address printf("x is %d; xPtr is %x; y is %d; yPtr is %x\n", *xPtr, xPtr, *yPtr, yPtr); // array manipulation printf("a[0] is %d, *aPtr is %d \n", a[0], *aPtr); aPtr += 2; // does NOT increment by integer 2, but by pointer arithmetic printf("a[0] is %d, *aPtr is %d \n", a[0], *aPtr); aPtr = a; // start at 0 again printf("*(aPtr+5) is %d\n", *(aPtr+5)); // int *bPtr; // no memory allocated // *bPtr = 6; // not allowed. // printf << "bptr is " << *bPtr << endl; // not allowed bPtr = (int *) malloc(sizeof(int)); // malloc sizeof is like new in Java // if an object, will call the default constructor *bPtr = 6; // now bPtr points to a nameless variable. no aliases. common printf("bptr is %d\n", *bPtr); free(bPtr); // gets rid of memory pointed to. must do in C } void read(int* x) { printf( "Enter a number in the pointer function-> "); scanf("%d", x); // no & because x is pointer }