From: Jonathan W <jwi...@ea...> - 2001-03-31 23:28:41
|
The following STL email requires knowledge of pointers and I think some other people besides just the STL branch needs to go over them. Don't expect to have a great grasp over pointers with just this lesson (for people that aren't already familiar with 'em), but with more examples you should be able to get the hang of it. A variable points to a memmory address. At the memory address, the actual data is stored. To be able to see what the memory address of a variable is you can: cout << &theVariable; The "&" or returns the memory address instead of the data that is contained in the address. When reading the operator, you should read it as "the address of". To be able to stored the memory address, you need to declare a pointer with the same type as the variable that you want to point to. You can declare something as a pointer by placing an asterisk infront of the variable: int x; int *x_ptr; // or for characters char MiddleInitial; char *MiddleInitial_ptr; To give the address of MiddleInitial to the pointer MiddleInitial_ptr do the following: MiddleInitial_ptr = &MiddleInitial; Preferrably by typing, recreate the example at the end of the email. Take note of what is printed out on the screen. It is *strongly* recommended that you make a similiar program for the experience. // BEGINNING OF EXAMPLE #include <iostream.h> int main () { int x; int *x_ptr; // x_ptr holds an address, not really int x=2; x_ptr=&x; // "&" in this case means "the address of" *x_ptr*=2; cout << "The value of x is " << x << "\n"; cout << "The address of x is " << &x << "\n"; cout << "The value of x_ptr is " << x_ptr << "\n"; cout << "The deference of x_ptr (*x_ptr) is " << *x_ptr << "\n"; return 0; } // END OF EXAMPLE -- Jonathan Williford _______________________________________________ cpptraining mailing list cpp...@ma... https://mail.worldforge.org/lists/listinfo/cpptraining |