Pointers

Image from ducatiperformanceparts.net accessed 5/1/17

This is a very complicated portion of the content and is something that Dr. Feinauer places a great emphasis on. Don’t be afraid at first you will spend lots of time learning the basics in class. Pointers are variables that can be assigned and reassigned within and through functions. & symbols are a point to where the data is stored. A * shows that a pointer is being initialized.

The best example

The best example of how pointer work comes from a forum, answers.yahoo.com (accessed 5/1/17). This story helped me organize how we see pointers since it is something that you can actually imagine!:

“Pointers: they “point” to locations in memory. Think of a row of safety deposit boxes—of various sizes—at a local bank. Each safety deposit box will have a number associated with it, so that the teller can quickly look it up. These numbers are like the memory addresses of variables. A pointer in the world of safety deposit boxes would simply be anything that stored the number of another safety deposit box.

Perhaps you have a rich uncle who stored valuables in his safety deposit box, but decided to put the real location in another, smaller, safety deposit box that only stored a card with the number of the large box with the real jewelery. The safety deposit box with the card would be storing the location of another box; it would be equivalent to a pointer. In the computer, pointers are just variables that store memory addresses, usually the addresses of other variables.

The cool thing is that once you can talk about the address of a variable, you’ll then be able to go to that address and retrieve the data stored in it. If you happen to have a huge piece of data that you want to pass into a function, it’s a lot easier to pass its location to the function than to copy every element of the data! Moreover, if you need more memory for your program, you can request more memory from the system—how do you get “back” that memory? The system tells you where it is located in memory. In other words, you get a memory address back. And you need pointers to store the memory address.”

Syntax

Int *number; declaring pointer integer

Char *character; declaring pointer character

Val = *whatsup; //val equal to the value pointed to by whatsup.

&whatsup = address where whatsup is stored

Let’s practice pointers in a simple code using the library iostream.

#include <iostream>

using namespace std;

int main(){

int firstvalue, secondvalue;

int * pointer;

pointer = &firstvalue;
*pointer = 10;

pointer = & secondvalue;
*pointer = 20;

cout<< “firstvalue is”<<firstvalue<<\n;
cout<< “secondvalue is”<<secondvalue<<\n;

return 0;}

Conclusion

As you can see the console prints “firstvalue is 10 secondvalue is 20” pointers can be called to in other functions within the parentheses of the function title. This is because the value of 20 is stored in the variable name (at the corresponding address) and that is printed to the screen.

Additional Information

http://www.cplusplus.com/doc/tutorial/pointers/

The course test material will also cover the content from this video:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s