| 1. |
pointer variable can be assigned the value of another pointer variable. int*ptr,*sptr;*ptr=&a;*sptr=*ptr; |
|
Answer» Answer: Pointer Basics What is a Pointer? A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory items. Pointers are very useful for another type of parameter passing, usually referred to as Pass By Address. Pointers are essential for dynamic memory allocation. Declaring pointers: Pointer declarations use the * operator. They follow this format: typeName * variableName; INT n; // declaration of a variable n int * p; // declaration of a pointer, called p In the example above, p is a pointer, and its type will be specifically be referred to as "pointer to int", because it stores the address of an integer variable. We also can say its type is: int* The type is important. While pointers are all the same size, as they just store a memory address, we have to know what kind of thing they are pointing TO. double * dptr; // a pointer to a double char * c1; // a pointer to a character float * fptr; // a pointer to a float Note: Sometimes the notation is confusing, because different TEXTBOOKS place the * differently. The three following declarations are equivalent: int *p; int* p; int * p; All three of these declare the variable p as a pointer to an int.
Another tricky aspect of notation: Recall that we can declare mulitple variables on one line under the same type, like this: int x, y, z; // three variables of type int Since the type of a "pointer-to-int" is (int *), we might ask, does this create three pointers? int* p, Q, r; // what did we just create? NO! This is not three pointers. Instead, this is one pointer and two integers. If you want to create mulitple pointers on one declaration, you must repeat the * operator each time: int * p, * q, * r; // three pointers-to-int int * p, q, r; // p is a pointer, q and r are ints Notation: Pointer dereferencing Once a pointer is declared, you can refer to the thing it points to, known as the target of the pointer, by "dereferencing the pointer". To do this, use the unary * operator: int * ptr; // ptr is now a pointer-to-int
// Notation: // ptr refers to the pointer itself // *ptr the dereferenced pointer -- refers now to the TARGET Suppose that ptr is the above pointer. Suppose it stores the address 1234. Also suppose that the integer STORED at address 1234 has the value 99. cout << "The pointer is: " << ptr; // prints the pointer cout << "The target is: " << *ptr; // prints the target // Output: // The pointer is: 1234 // exact printout here may vary // The target is: 99 Note: the exact printout of an addres may vary based on the system. Some systems print out addresses in hexadecimal notation (base 16). Note: The notation can be a little confusing. If you SEE the * in a declaration statement, with a type in front of the *, a pointer is being declared for the first time. AFTER that, when you see the * on the pointer name, you are dereferencing the pointer to get to the target. Pointers don't always have valid targets. A pointer refers to some address in the program's memory space. A program's memory space is divided up into segements Each memory segment has a different purpose. Some segments are for data storage, but some segments are for other things, and off limits for data storage If a pointer is pointing into an "out-of-bounds" memory segment, |
|