Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><b>TL;DR</b>: pointers are useful when multiple places need access to the same information</p> <p>In your example they aren't doing much, like you said it's just showing how they can be used. One thing pointers are used for is to connect nodes like in a tree. If you have a node structure like so...</p> <pre><code>struct myNode { myNode *next; int someData; }; </code></pre> <p>You can create several nodes and link each one to the previous myNode's <code>next</code> member. You can do this without pointers, but the neat thing with pointers is because they are all linked together, when you pass around the myNode list you only need to pass the first (root) node.</p> <p>The cool thing about pointers is that if two pointers are referencing the same memory address, any changes to the memory address are recognized by everything referencing that memory address. So if you did:</p> <pre><code>int a = 5; // set a to 5 int *b = &amp;a; // tell b to point to a int *c = b; // tell c to point to b (which points to a) *b = 3; // set the value at 'a' to 3 cout &lt;&lt; c &lt;&lt; endl; // this would print '3' because c points to the same place as b </code></pre> <p>This has some practical uses. Consider you have a list of nodes linked together. The data in each node defines some sort of task that needs to be done that will be handled by some function. As new tasks are added to the list, they get appended to the end. Since the function has a pointer to the node list, as tasks are added on it receives those as well. On the other hand, the function can also remove tasks as it completes them, which are then reflected back across any other pointers that are looking at the node list.</p> <p>Pointers are also used for dynamic memory. Say you want the user to enter in a series of numbers, and they tell you how many numbers they want to use. You could define an array of 100 elements to allow for up to 100 numbers, or you could use dynamic memory.</p> <pre><code>int count = 0; cout &lt;&lt; "How many numbers do you want?\n&gt; "; cin &gt;&gt; count; // Create a dynamic array with size 'count' int *myArray = new int[count]; for(int i = 0; i &lt; count; i++) { // Ask for numbers here } // Make sure to delete it afterwars delete[] myArray; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload