Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //----------------------------------
- // Week 6 : Pointers
- //----------------------------------
- // I remembered when I was studying object-oriented at the university level and
- // had visited the student tutor.
- // I had asked him to explain what is a pointer and can you explain it to me?
- // The only thing I recalled from that particular lesson was
- // "follow the pointer". Yeah...That wasn't really that helpful.
- // To be honest, it took me a while to figure out what a pointer truly meant.
- // Even today, I think my grasp of pointers is pretty good,
- // but my mastery of advanced level of pointer still needs work.
- // Let's start with the fundamentals first.
- // What is a pointer?
- // The best metaphor that I came up with is, a pointer is a finger and nothing more.
- // A finger can point at things and tell you what's at that specific location.
- // When it comes to development, a pointer is meant to "point" at a specific address and
- // tell you what's at that address.
- // Is it an integer, is it a character, or is it a string? Who knows.
- // What is the purpose of a pointer?
- // To conserve memory and make efficient programs.
- // Instead of working with actual data, wouldn't it be
- // easier just to point at what you need instead?
- // What I didn't know until recently is that a pointer can only point to three things.
- // A pointer can only point at another address, another pointer, or a null value.
- // You can't have a pointer point at something else
- // besides these three.
- // So, let's start.
- // Declaring a pointer
- // There are three ways to declare a pointer, but they all mean the same thing.
- // int* pointer1;
- // int * pointer1;
- // int *pointer1;
- // I generally like the first declaration of the pointer.
- // What you see here is a "*" symbol, correct?
- // That just means that you have declared a pointer that is pointing to an int dataspace.
- // So, you must be dealing with integer only.
- // Intializing a pointer
- // int* pointer1 = 5;
- // pointer1 is pointing to the value of 5.
- // pointer1 is not the value of 5, but rather just points to it.
- // std::cout << pointer1;
- // When we perform this, we can see that pointer1 is the address of the value 5.
- // std::cout << *pointer1;
- // When we dereference pointer1 with *, we can now go to the address of pointer1 and
- // see that there is a value of 5 at that address.
- // Exercises
- //
- // How to move a pointer
- // Now, we see that pointers can be used to point at a single object.
- // How about we see how pointers are used on a larger scale.
- int main()
- {
- // declare out first array
- int array[5] = {0,1,3,4,5};
- // declare our pointer and have intialize it to our array.
- // pointer1 is now equals to array.
- int* pointer1 = array;
- // This should output the first element of the array
- std::cout << *pointer1;
- // Now can you guess what these numbers are?
- std::cout << *(pointer1 + 2);
- std::cout << pointer1[3];
- }
- // Exercises Math Pointers****
- // How to use a pointer in a function
- // Pointer Math Exercises
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement