![]() ![]() | ||||
![]() ![]() |
Lesson 6: An
introduction to pointers (Printable
Version) #include <iostream.h> int main() { int x; //A normal integer int *pointer; //A pointer to an integer pointer=&x; //Read it, "pointer equals the address of x" cin>>x; //Reads in x cout<<*pointer; //Note the use of the * to output the actual number stored in x return 0; } The cout outputs the value in x. Why is that? Well, look at the code. The integer is called x. A pointer to an integer is then defined as "pointer". Then it stores the memory location of x in pointer by using the ampersand (&) symbol. If you wish, you can think of it as if the jar that had the integer had a ampersand in it then it would output its name (in pointers, the memory address) Then the user inputs the value for x. Then the cout uses the * to put the value stored in the memory location of pointer. If the jar with the name of the other jar in it had a * in front of it would give the value stored in the jar with the same name as the one in the jar with the name. It is not too hard, the * gives the value in the location. The unasterisked gives the memory location. Notice that in the above example, pointer is initialized to point to a specific memory address before it is used. If this was not the case, it could be pointing to anything. This can lead to extremely unpleasant consequences to the computer. You should always initialize pointers before you use them. It is also possible to initialize pointers using free memory. This allows dynamic allocation of array memory. It is most useful for setting up structures called linked lists. This difficult topic is too complex for this text. An understanding of the keywords new and delete will, however, be tremendously helpful in the future. The keyword new is used to initialize pointers with memory from free store (a section of memory available to all programs). The syntax looks like the example: Example: int *ptr = new int; It initializes ptr to point to a memory address of size int (because variables have different sizes, number of bytes, this is necessary). The memory that is pointed to becomes unavailable to other programs. This means that the careful coder will free this memory at the end of its usage. The delete operator frees up the memory allocated through new. To do so, the syntax is as in the example. Example: delete ptr; After deleting a pointer, it is a good idea to reset it to point to NULL. NULL is a standard compiler-defined statement that sets the pointer to point to, literally, nothing. By doing this, you minimize the potential for doing something foolish with the pointer. The final implication of NULL is that if there is no more free memory, it is possible for the ptr after being "new"-ed to point to NULL. Therefore, it is good programming practice to check to ensure that the pointer points to something before using it. Obviously, the program is unlikely to work without this check. Quiz yourself Previous: Switch/case Next: Structures ----- |
| ||
|