===============================MODULE8===================================== | | | The program examples' source codes have been arranged in the same | | order that appeared in the Tutorial. This is unedited and unverified | | compilation. Published as is basis for educational, reacretional and | | brain teaser purposes. All trademarks, copyrights and IPs, wherever | | exist, are the sole property of their respective owner and/or | | holder. Any damage or loss by using the materials presented in this | | tutorial is USER responsibility. Part or full distribution, | | reproduction and modification is granted to any body. | | Copyright 2003-2005 © Tenouk, Inc. All rights reserved. | | Distributed through http://www.tenouk.com | | | | | =========================================================================== Originally programs compiled using Borland C++. Examples compiled using VC++/VC++ .Net and gcc or g++ are given at the end of every Module. For example if you want to compile C++ codes using VC++/VC++ .Net, change the header file accordingly. Just need some modification for the header files...: ------------------------------------------------- #include //for system() #include ... { C++ codes... } ------------------------------------------------- should be changed to: ------------------------------------------------- #include //use C++ wrapper to call C functions from C++ programs... #include using namespace std; ... { C++ codes... } ------------------------------------------------- In VC++/VC++ .Net the iostream.h (header with .h) is not valid anymore. It should be C++ header, so that it comply to the standard. In older Borland C++ compiler this still works, but not proper any more... and for standard C/C++ the portability should be no problem or better you read Module23 at http://www.tenouk.com/Module23.html to get the big picture...For C codes, they still C codes :o) ========================================================================= ========================================================================= #include #include int main(void) { int *m; int location = 200; m = &location; printf("The data, *m = %d\n",*m); printf("The address where the data pointed to, m = %d\n", m); system("pause"); return 0; } ----------------------------------------------------------------------------------- // program to illustrate the basic use of pointers //**********C++***************** #include // using C header in C++ #include using namespace std; void main() { //declares an integer variable and two pointers variables int num = 10, *point_one, *point_two; //assigns the address of variable num to pointer point_one point_one = # //assigns the (address) point_one to point_two point_two = point_one; cout<<"Pointers variables..."< #include void main() { //Declare and initialize an int variable int var = 34; //Declare a pointer to int variable int *ptr; //Initialize ptr to point to variable var ptr = &var; //Access var directly and indirectly printf("\nDirect access, variable var value = var = %d", var); //you can use %p for the pointer memory address directly or //%0x or %0X in hexadecimal representative instead of //%d, just to avoid confusion here… printf("\nIndirect access, variable var value = *ptr = %d", *ptr); //Display the address of var two ways printf("\n\nThe memory address of variable var = &var = %d", &var); printf("\nThe memory address of variable var = ptr = %d\n", ptr); system("pause"); } -------------------------------------------------------------------------------------- #include #include int main() { int *thepointer; thepointer = NULL; //do some testing.... printf("The thepointer pointer is pointing to = %X\n", thepointer); printf("The thepointer pointer is pointing to = %d\n", thepointer); system("pause"); return 0; } -------------------------------------------------------------------------------------- //Array, pointer and string #include void main() { //an array variable of type char, sized 79 char sentence[80]; //prompt for user input... printf("Enter a line of text:\n"); //read the user input... gets(sentence); //display what has been read by gets() printf("Line of text entered: \n%s\n", sentence); getchar(); } ---------------------------------------------------------------------------------------- //demonstrates the relationship between addresses //and elements of arrays of different data type #include #include void main() { //declare three arrays and a counter variable int i[10], x; float f[10]; double d[10]; //print the table heading printf("\nArray's el. add of i[x] add of f[x] add of d[x]"); printf("\n|================================"); printf("======================|"); //print the addresses of each array element for(x=0; x<10; x++) printf("\nElement %d:\t%p\t%p\t%p",x,&i[x],&f[x],&d[x]); printf("\n|================================"); printf("======================|\n"); printf("\nLegends:"); printf("\nel.- element, add - address\n"); printf("\ndifferent pc, shows different addresses\n"); system("pause"); } --------------------------------------------------------------------------------------------- //demonstrates the use of pointer arithmetic to access //array elements with pointer notation #include #include #define MAX 10 void main() { //declare and initialize an integer array int array1[MAX] = {0,1,2,3,4,5,6,7,8,9}; //declare a pointer to int and an int variable int *ptr1, count; //declare and initialize a float array float array2[MAX] = {0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9}; //declare a pointer to float float *ptr2; //initialize the pointers //just an array name is the pointer to the //1st array element, both left value and right value //of the expression are pointers types... ptr1 = array1; ptr2 = array2; //print the array elements printf("\narray1 values array2 values"); printf("\n-------------------------"); //iterate or loop the arrays and display the content... for(count = 0; count < MAX; count++) printf("\n%d\t\t%f", *ptr1++, *ptr2++); printf("\n-------------------------\n"); system("pause"); } ----------------------------------------------------------------------------------------------- //program that passes a pointer array to a function #include #include //function prototype for viewArray void viewArray(int *[]); void main() { //declare and initialize the array variables... int i,*arrayPtr[7], var[7]={3,4,4,2,1,3,1}; //loop through the array... for(i=0; i<7; i++) //arrayPtr[i] is assigned with //the address of var[i] arrayPtr[i] = &var[i]; //A call to function viewArray, //pass along the pointer to the //1st array element viewArray(arrayPtr); cout< /*or int main(int argc, *argv[])*/ int main(int argc, char **argv) { int i; printf("argc = %d\n\n", argc); for (i=0; i #include int main(void) { int **theptr; int *anotherptr; int data = 200; anotherptr = &data; // assign the second pointer address to // the first pointer... theptr = &anotherptr; printf("The actual data, **theptr = %d\n", **theptr); printf("\nThe actual data, *anotherptr = %d\n", *anotherptr); printf("\nThe first pointer pointing to an address, theptr = %p\n", theptr); printf("\nThis should be the second pointer address, &anotherptr = %p\n", &anotherptr); printf("\nThe second pointer pointing to address(= hold data),\nanotherptr = %p\n", anotherptr); printf("\nThen, its own address, &anotherptr = %p\n", &anotherptr); printf("\nThe address of the actual data, &data = %p\n", &data); printf("\nNormal variable, the data = %d\n", data); system("pause"); return 0; } ----------------------------------------------------------------------------------------------- /* Invoking function using function pointer */ #include int somedisplay(); int main() { int (*func_ptr)(); /* assigning a function to function pointer as normal variable assignment */ func_ptr = somedisplay; /* checking the address of function */ printf("\nAddress of function somedisplay() is %p", func_ptr); /* invokes the function somedisplay() */ (*func_ptr)() ; return 0; } int somedisplay() { printf("\n--Displaying some texts--\n"); return 0; } ------------------------------------------------------------------------------------------------- #include /* function prototypes */ void funct1(int); void funct2(int); /* making FuncType an alias for the type 'function with one int argument and no return value'. This means the type of func_ptr is 'pointer to function with one int argument and no return value'. */ typedef void FuncType(int); int main(void) { FuncType *func_ptr; /* put the address of funct1 into func_ptr */ func_ptr = funct1; /* call the function pointed to by func_ptr with an argument of 100 */ (*func_ptr)(100); /* put the address of funct2 into func_ptr */ func_ptr = funct2; /* call the function pointed to by func_ptr with an argument of 200 */ (*func_ptr)(200); return 0; } /* function definitions */ void funct1 (testarg) {printf("funct1 got an argument of %d\n", testarg);} void funct2 (testarg) {printf("funct2 got an argument of %d\n", testarg);} ------------------------------------------------------------------------------------------------ /* An array of pointers to function */ #include /* functions' prototypes */ int fun1(int, double); int fun2(int, double); int fun3(int, double); /* an array of a function pointers */ int (*p[3]) (int, double); int main() { int i; /* assigning address of functions to array pointers */ p[0] = fun1; p[1] = fun2; p[2] = fun3; /* calling an array of function pointers with arguments */ for(i = 0; i <= 2; i++) (*p[i]) (100, 1.234); return 0; } /* functions' definition */ int fun1(int a, double b) { printf("a = %d b = %f", a, b); return 0; } int fun2(int c, double d) { printf("\nc = %d d = %f", c, d); return 0; } int fun3(int e, double f) { printf("\ne = %d f = %f\n", e, f); return 0; } -------------------------------------------------------------------------------------------------- // pointer to a function #include #include // function prototypes... float minimum(float, float); // (*ptr) is a pointer to function of type float float (*ptr)(float, float); void main() { float x1, x2, small; // Assigning address of minimum() function to ptr ptr = minimum; cout<<"\nEnter two numbers, separated by space: "; cin>>x1>>x2; // call the function pointed by ptr small // has the return value small = (*ptr)(x1, x2); cout<<"\smaller number is "< /* void pointer */ int func(void *thePtr); int main() { /* assigning a string to the pointer */ char *theStr = "abcd1234"; //function call func(theStr); return 0; } int func(void *thePtr) { printf("%s\n", thePtr); return 0; } ---------------------------------------------------------------------------------------------------- //Program that changes the value of a pointer variable #include using namespace std; void main() { //declare and initialize two //float variables float var1 = 58.98; float var2 = 70.44; //declare a float pointer variable float *ptr_var; //make ptr_var point to variable var1... ptr_var = &var1; //prints 58.98 cout<<"\nThe first value is(var1) "<<*ptr_var; cout<<"\nThe address of the first data is "< using namespace std; void main() { int x = 4, y = 7; //function prototype... void addcon(int*, int*); cout<<"\nInitial value of x = "< using namespace std; void main() { float temper[40], sum = 0.0, *ptr; int num, day = 0; //set a pointer to an array... ptr = temper; do { cout<<"Enter temperature for day "<<++day; //prompt for input user input... cout<<"\n(0-Terminate, Enter-Proceed): "; //store in an array, pointed by ptr... cin>>*ptr; } while ((*ptr++) > 0); //Test if data entered is 0, //then point to the next array position //reset the pointer ptr to an array temper ptr = temper; num = (day – 1); //looping through the array temper... for(day = 0; day < num; day++) //do the summing up... sum += *(ptr++); //display the result... cout<<"\nAverage temperature = "<<(sum/num)<<" Degree Celsius"< #include void main() { int i, offset, b[] = {10, 20, 30, 40}; //set bPtr to point to array b int *bPtr = b; printf("So many notations?????....\n"); //....separating code in multiple lines printf("Array b printed with: \n" "Array subscript notation\n"); for(i=0; i<=3; i++) printf("b[%d] = %d\n", i, b[i]); printf("\nPointer/offset notation where \n" "the pointer is the array name\n"); for(offset = 0; offset <=3; offset++) printf("*(b + %d) = %d\n", offset, *(b + offset)); printf("\nPointer subscript notation\n"); for(i=0; i<=3; i++) printf("bPtr[%d] = %d\n",i,bPtr[i]); printf("\nPointer/offset notation\n"); for(offset = 0; offset <=3; offset++) printf("*(bptr + %d) = %d\n", offset, *(bPtr + offset)); system("pause"); } ---------------------------------------------VC++/VC++ .Net----------------------------------------------------- //Program that changes the value of a pointer variable //compiled using VC++/VC++ .Net, C++ codes… #include using namespace std; void main() { //declare and initialize two //float variables double var1 = 58.98; double var2 = 70.44; //declare a float pointer variable double *ptr_var; //make ptr_var point to variable var1... ptr_var = &var1; //prints 58.98 cout<<"The first value is(var1) "<<*ptr_var; cout<<"\nThe address of the first data is "< int main() { int i, offset, b[] = {10, 20, 30, 40}; //set bPtr to point to array b int *bPtr = b; printf("So many notations?????....\n"); //....separating code in multiple lines printf("Array b printed with: \n" "Array subscript notation\n"); for(i=0; i<=3; i++) printf("b[%d] = %d\n", i, b[i]); printf("\nPointer/offset notation where \n" "the pointer is the array name\n"); for(offset = 0; offset <=3; offset++) printf("*(b + %d) = %d\n", offset, *(b + offset)); printf("\nPointer subscript notation\n"); for(i=0; i<=3; i++) printf("bPtr[%d] = %d\n", i, bPtr[i]); printf("\nPointer/offset notation\n"); for(offset = 0; offset <=3; offset++) printf("*(bptr + %d) = %d\n", offset, *(bPtr + offset)); return 0; } ------------------------------------------G++ Linux/Fedora-------------------------------------------------------- /////////////////funcref.cpp////////////////// //Illustrates a function that receives addresses //of variables and then alters their contents #include using namespace std; int main() { int x = 4, y = 7; //function prototype... void addcon(int*, int*); cout<<"\nInitial value of x = "<