Basics of C/C++ Part 6: Intro to Pointers

Submitted on: 1/1/2015 7:38:00 AM
By: Alexander of (from psc cd)  
Level: Intermediate
User Rating: By 7 Users
Compatibility: C, C++ (general)
Views: 1776
     Welcome to the sixth in my series of tutorials. This is one about a topic that you may or may not have already heard about...pointers. What are they, what do they do, why do we care? First, why do we care. We care about pointers because they allow access to memory, the also make array-access faster, they are also somewhat necessary to understand some functions. Most importantly, you will see them a lot in other people's code. You may not need to use them much, but it will be tremendously important to understand them.


Second, what do they do. Well, I might as well address this issue as well as what they are at the same time. Pointers are what they sound like...pointers. They point to locations in memory. Picture this: a big jar that holds one thing, the name of another jar. In the other jar is the value of an integer. The jars are memory locations. The jar that holds the name of the other jar is a pointer. It points to the other drawer.

How can you use this? Well, look at this little piece of code:


void main()

int x;
int *pointer;



Guess what! The cout outputs the value in x. Why is that? Well, look at the code. The integer is called x. Then a pointer to an integer is defined as pointer. The astrick(*) symbol means that it is a pointer. Then I give the memory location of x to pointer by using the ampersand(&) symbol. It gives the memory location of the variable it is in front of. For example, if the jar that had an integer had a ampersand in it it would output its name, or location. Then the user inputs the value for x. Then the cout uses the * to put the value stored in the memory location of pointer. Huh? Picture the jars again. If the jar with the name of the other jar in it had a * in front of it it would give the value stored in the jar with the same name as the one in the jar with the name. It's not too hard, the * gives the value in the location. The unastricked gives the memory location.

I hope this has been at least an interesting introduction to pointers. I do not suggest that you play around with them too much as you can do unpleasant things on your computer, but you now should have a better understand of what they are.

Other 10 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.