Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Why are pointers such a leading factor of confusion for many new, and even old, college level students in the C/C++ language? </p> </blockquote> <p>The concept of a placeholder for a value - variables - maps onto something we're taught in school - algebra. There isn't an existing parallel you can draw without understanding how memory is physically laid out within a computer, and no one thinks about this kind of thing until they're dealing with low level things - at the C/C++/byte communications level.</p> <blockquote> <p>Are there any tools or thought processes that helped you understand how pointers work at the variable, function, and beyond level?</p> </blockquote> <p>Addresses boxes. I remember when I was learning to program BASIC into microcomputers, there were these pretty books with games in them, and sometimes you had to poke values into particular addresses. They had a picture of a bunch of boxes, incrementally labelled with 0, 1, 2... and it was explained that only one small thing (a byte) could fit in these boxes, and there were a lot of them - some computers had as many as 65535! They were next to each other, and they all had an address.</p> <blockquote> <p>What are some good practice things that can be done to bring somebody to the level of, "Ah-hah, I got it," without getting them bogged down in the overall concept? Basically, drill like scenarios.</p> </blockquote> <p>For a drill? Make a struct:</p> <pre class="lang-cpp prettyprint-override"><code>struct { char a; char b; char c; char d; } mystruct; mystruct.a = 'r'; mystruct.b = 's'; mystruct.c = 't'; mystruct.d = 'u'; char* my_pointer; my_pointer = &amp;mystruct.b; cout &lt;&lt; 'Start: my_pointer = ' &lt;&lt; *my_pointer &lt;&lt; endl; my_pointer++; cout &lt;&lt; 'After: my_pointer = ' &lt;&lt; *my_pointer &lt;&lt; endl; my_pointer = &amp;mystruct.a; cout &lt;&lt; 'Then: my_pointer = ' &lt;&lt; *my_pointer &lt;&lt; endl; my_pointer = my_pointer + 3; cout &lt;&lt; 'End: my_pointer = ' &lt;&lt; *my_pointer &lt;&lt; endl; </code></pre> <p>Same example as above, except in C:</p> <pre class="lang-c prettyprint-override"><code>// Same example as above, except in C: struct { char a; char b; char c; char d; } mystruct; mystruct.a = 'r'; mystruct.b = 's'; mystruct.c = 't'; mystruct.d = 'u'; char* my_pointer; my_pointer = &amp;mystruct.b; printf("Start: my_pointer = %c\n", *my_pointer); my_pointer++; printf("After: my_pointer = %c\n", *my_pointer); my_pointer = &amp;mystruct.a; printf("Then: my_pointer = %c\n", *my_pointer); my_pointer = my_pointer + 3; printf("End: my_pointer = %c\n", *my_pointer); </code></pre> <p>Output:</p> <pre><code>Start: my_pointer = s After: my_pointer = t Then: my_pointer = r End: my_pointer = u </code></pre> <p>Perhaps that explains some of the basics through example?</p>
    singulars
    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.
    3. 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