C++/Pointers

Advertisement


Question
Hey, I'm having some trouble understanding the pointer, can you help clarify, thanks?


1) Assuming p to be a pointer, *p = 100 means value of p = 100 or the value of the variable pointed by p is 100?
2) Assuming p to be a pointer, p = p + 9 makes p point to the ninth element of p's base type, beyond the one to which it is currently pointing. What does this mean? p now contains the value of the ninth value of the variable pointed to?
3) What is the difference between i = g, and *i = g?


Answer
Hello again, Angela!  I'm happy to help you with pointers.  I love pointers. :)

1. When you dereference a pointer (the * operator), you are instructing the system to access what the pointer is pointing to.  So dereferencing p in that instance means you are setting the value of the variable pointed to by p to 100.  If you wanted to set the actual variable p to 100, you would simply write p = 100.  Basically, if you don't dereference it, you're manipulating the pointer.  If you do dereference it, you're manipulating the data that the pointer points to.

2. In this case, p does not contain the value of the ninth index, but points to the ninth index.  Pointer arithmetic is one of the best things about pointers.  The easiest way to understand it is to reference something you are likely very familiar with -- arrays.  In truth, an array is just a pointer.  The following lines are (mostly) equivalent:

   int myArray[20];
   int* myArray = new int[20];

In both cases, a piece of contiguous memory large enough to hold 20 integers has been set aside.  In both cases you may access each index with the [] operator.  

So, this brings us back to p = p + 9.  When you add to a pointer, you are shifting the address by the size of a single element of what the pointer points to times the number you're adding.  What this means, is, using the integer array as an example, doing this:

   int* pNewPointer = myArray + 9;

would set pNewPointer to point to the ninth index in the array.  The difference between the address in myArray and pNewPointer is sizeof(int) * 9.  And, using the above statement as a reference, the code in your question, p = p + 9, would set p to point to the 9th index.

3. As I stated in the answer to question 1, when you dereference a pointer you are accessing the data that the pointer points to, and when you do not dereference the pointer, you are changing the pointer itself.  So i = g is changing where i points to, while *i = g is changing the value of the data at the address that i points to.

As always, if any of this is unclear or you otherwise have further questions, do not hesitate to ask.  Thanks!

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C++ code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2016 About.com. All rights reserved.