![]() |
What is a Pointer?A pointer is a 4 byte address that points to a location in memory. Why are Pointers used?Everytime you declare a variable, space is allocated on the stack. This space is limited and therefore you could run out of space on the stack when creating too many variables or variables of great size. Pointers allow you to reference data stored in memory which has a much greater capacity. The access speed is also much greater. You are therefore left with one 4 byte pointer on the stack which points to a large amount of data in memory. How are Pointers declared?
A pointer takes the form of : Contents of main.cpp : #include <iostream> #include <stdlib.h> using namespace std; int main() {
You usually need to allocate space in memory when wanting to point to
new data. This will be covered in a later tutorial. // Different Character Pointers //------------------------------ char *pointerToString = "Hello"; // pointerToString[3] = 'r'; - RUNTIME ERROR
To prevent yourself from getting runtime errors created by the above problem, you
can define all strings as being a const char *. const char *pointerToConstantString = "Hello"; // pointerToConstantString[3] = 'r'; // COMPILE ERROR If you want to create a pointer that can only ever point to one location, you can include the const keyword after the *. In our example, it will prevent you from reassigning a different string to the variable. char * const constantPointerToString = "Hello"; // constantPointerToString[1] = 't'; // RUNTIME ERROR // constantPointerToString = "Hello2"; // COMPILE ERROR This can be combined with our method of avoiding runtime errors as shown below. const char * const constantPointerToConstantString = "Hello"; //constantPointerToConstantString[2] = 'r'; // COMPILE ERROR //constantPointerToConstantString = "Hello2"; // COMPILE ERROR The cout object knows to output the data when a character pointer is given to it. cout << pointerToConstantString << endl; A few pointers have been defined to help us with the rest of the tutorial. The keyword NULL is used to indicate nothing. The px pointer therefore points to nothing. // Pointer Declarations //---------------------- int x = 0; int *px = NULL; char *str = "Hello World"; The following piece of code shows what happens when you output the size of a pointer. It should display both as being 4 bytes. It is important to note that when using the sizeof method on a char *, the length of the string is NOT outputted. // Size of pointers //---------------------- cout << "Size of pointer : " << sizeof(px) << endl; cout << "Size of str : " << sizeof(str) << endl; The next piece of code shows what happens when you output pointers. x outputs 0 as it is a normal integer that has been assigned the value of 0. px however, outputs a number of 0s. This is because it is an address (NULL address). // Values //-------- cout << "Value of x : " << x << endl; cout << "Value of px : " << px << endl;
If you want to use the address of a variable, you need to place a &
before the variable name. // Addresses //----------- cout << "Address of x : " << &x << endl; cout << "Address of px : " << &px << endl;
If you would like to receive that actual value that is being pointed to, you need to
place the * (dereference) operator before the variable name.
str is a pointer to the first character of a string. *str
will therefore output the first character which is in this case 'H'.
// Information pointed to //------------------------ cout << "Value pointed to by str : " << *str << endl; cout << "Value pointed to by str+1 : " << *(str + 1) << endl; system("pause"); return 0; } You should now know what a pointer is and how it can be used. Pointers are used in almost every C / C++ application. We have not covered everything in connection with pointers but we will revisit them in future tutorials. Please let me know of any comments you may have : Contact Me
All Rights Reserved, © Zeus Communication, Multimedia & Development 2004-2005 Read the Disclaimer |
|