{"id":15,"date":"2012-11-21T01:24:00","date_gmt":"2012-11-21T01:24:00","guid":{"rendered":""},"modified":"2018-12-15T20:06:37","modified_gmt":"2018-12-15T20:06:37","slug":"c-pointers-explained-really","status":"publish","type":"post","link":"https:\/\/karwin.com\/blog\/index.php\/2012\/11\/21\/c-pointers-explained-really\/","title":{"rendered":"C Pointers Explained, Really"},"content":{"rendered":"<p>While I was in college, a friend of mine complained that he was confused while programming in C, struggling to learn the syntax for pointers.<\/p>\n<p>He gave the example of something like: <tt>*x=**p++<\/tt> being ugly and unreadable, with too many operations layered on each other, making it hard to tell what was happening. \u00a0He said he had done a bit of programming with assembly language, but he wasn&#8217;t accustomed to the nuances of C.<\/p>\n<p>I wrote the following explanation on our student message board, and I got a lot of good feedback. \u00a0Some people said that they had been programming in C for years, but not until they read my post did they finally understand pointers. \u00a0So here it is, unearthed from my backups and slightly edited. \u00a0I hope it helps someone again&#8230;<\/p>\n<hr \/>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">Message 1956 (8 left): Thu Jan 25 1990 2:44am<br \/>\nFrom: Bill! (easterb@ucscb)<br \/>\nSubject: Okay<\/p>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">Well, if you know assembly, you have a head start\u00a0on many of the CS freshpersons here. You at least know\u00a0about memory maps: RAM is a long long array of bytes.\u00a0It helped me to learn about pointers if I kept this in mind.\u00a0For some reason, books and instructors talking about\u00a0pointers want to overlook this.<\/p>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">When I have some code:<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">main()\n{\n  int n;\n  int *p;<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">There is a place in my memory that looks like this:<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">            :\nAddress:    :\n         |-----|\n0x5100   |     | n is an integer, one machine word big\n         |-----|\n0x5104   |     | p is a pointer, also one word big\n         |-----|\n0x5108   |     | other unused memory\n         |-----|\n            :\n            :<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">Let&#8217;s give these variables some values.\u00a0I set n to be the number 151.<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">n = 151;<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">I set the pointer p to point to the integer n.<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">p = &amp;n;<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">That says, &#8220;the value of the variable p is assigned the\u00a0address of the variable n&#8221;.<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">           :\nAddress:   :    Value at that address:\n         |----|\n0x5100   | 151| n\n         |----|\n0x5104   |5100| p\n         |----|\n0x5108   | ?  |\n         |----|\n           :\n           :<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">Now I want to print out the value of n, by two ways.<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">printf(\"n is %d.n\", n);\nprintf(\"n is %d.n\", *p);<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">The * operator says, &#8220;give me the object at the following address.&#8221;\u00a0The object&#8217;s type is the type that the pointer was declared as.\u00a0So, since we declared &#8220;int *p&#8221;, the object pointed at will be\u00a0_assumed_ by C to be an int. In this case, we were careful to\u00a0make this coincide with what we were pointing at.<\/p>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">Now I want to print out the memory address of n.<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">printf(\"n is located at $%x.n\", &amp;n);\nprintf(\"n is located at $%x.n\", p);<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">The &amp; operator says, &#8220;tell me the address where the following object\u00a0starts.&#8221; In this case, it is hex 5100 (I put a &#8216;$&#8217;\u00a0before it, to\u00a0conform to the Assembly notation I am used to).\u00a0Notice the _value_ of p is an address.<\/p>\n<p>Hm. Does p have an address? Sure. It is a variable, and all\u00a0variables have their own address. The address of p is hex 5104.<\/p>\n<pre>printf(\"p is located at $%x.n\", &amp;p);<\/pre>\n<p>Here we are taking the address of a pointer variable,\u00a0using the &amp; operator.<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">main()\n{\nchar name[] = \"Bill\";\nchar *p;\nint *q;<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">Now we have an array to play with. Here&#8217;s how memory looks now:<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">       |---|\n0x5100 |'B'| \"name\" is an address constant that has value hex 5100\n       |---|\n0x5101 |'i'| char: 1 byte\n       |---|\n0x5102 |'l'| char: 1 byte\n       |---|\n0x5103 |'l'| char: 1 byte\n       |---|\n0x5104 |   | char: 1 byte\n       |---|\n0x5105 |   | p is a pointer: 1 word\n       |---|\n0x5109 |   | q is a pointer: 1 word\n       |---|<\/pre>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">p = name;<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">We set p to the value of name. Now p has value hex 5100 too.\u00a0We can use the * dereferencing operator on p, and get the\u00a0character &#8216;B&#8217; as a result.<\/p>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">Now what happens if I do this:<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">++p;<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">The pointer p is incremented. What value does it have now?\u00a0Hex 5101. Pretty simple.<\/p>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">Now let&#8217;s try something irresponsible:<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">q = name;<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">But q is a pointer to int! If we dereference q, it will take\u00a0the word (typically 4 bytes) beginning at address &#8220;name&#8221; (which\u00a0is hex 5100) and try to convert it to an int. &#8216;B&#8217;, &#8216;i&#8217;, &#8216;l&#8217;, &#8216;l&#8217;\u00a0converted to an int will be some large number, dependant on the\u00a0bit-ordering algorithm on your machine&#8217;s architecture. On ucscb,\u00a0it becomes 1114205292. (to see how, line up the binary representation\u00a0of the ascii values for those 4 characters, and then run the 32 bits\u00a0together, and convert that resultant binary number as an integer.)<\/p>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">What we have just seen here is a key issue of pointers that I\u00a0mentioned earlier: C assumes that what they are pointing at\u00a0is an object of the type that the pointer was designed to point at.\u00a0It is up to the programmer to make sure this happens correctly.<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">++q;<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">The int pointer is incremented. What value does it have now?\u00a0Hex 5104. Huh?!? The answer is simple if you accept the above\u00a0paragraph. It gets incremented by the size of the object it\u00a0_thinks_ it is pointing at. It&#8217;s an int pointer, so incrementing\u00a0it makes it advance a number of bytes equal to the size of an int.<\/p>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">Now print the dereferenced value of q (i.e. the value of the object\u00a0q is pointing to). Well, it&#8217;s pointing at a null byte, and then\u00a0the first 3 bytes of the char *p. Now we&#8217;re all messed up.\u00a0Nice going. Try to convert _that_ to an integer representation.\u00a0Well actually, C will do it happily. But it&#8217;ll be another weird\u00a0number.<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">main()\n{\n  int n;\n\n  n = 151;\n  f(n);\n}\n\nf(x)\nint x;\n{\n  printf(\"%d.n\", x);\n}<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">Here is a simple program that passes an int &#8220;by value&#8221;.\u00a0That is, it copies the value of n into the new variable x!<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">       |---|\n0x5100 |151| n is an integer\n       |---|\n0x5104 |151| x is another integer\n       |---|<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">When we mention x, we are using the value at location 5104,\u00a0and we can change it, read it, whatever, and it won&#8217;t affect n,\u00a0the int at location 5100.<\/p>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">But what if we want to have f() modify the value and then\u00a0have that new value be available in main()? C does this by\u00a0passing the variable &#8220;by reference&#8221;.<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">main()\n{\nint n;\n\n  n = 151;\n  f(&amp;n);\n}\n\nf(x)\nint *x;\n{\n  printf(\"%d.n\", *x);\n  *x = 451;\n}<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">Pass the _address_ of n, and declare x as a _pointer_ to int.\u00a0Actually, this is still passing by value, but the value being\u00a0passed is the address, not the number.<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">       |----|\n0x5100 | 151| n is an integer\n       |----|\n0x5104 |5100| x is a pointer to int\n       |----|<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">Now if f() when we make use of *x, we are referring to the\u00a0value at location 5100. This is the location of n.\u00a0After the assignment &#8220;*x = 451;&#8221;, this is what we have:<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">       |----|\n0x5100 | 451| n is an integer\n       |----|\n0x5104 |5100| x is a pointer to int\n       |----|<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">x still points to location 5100, but we have changed the value\u00a0of the object at that location.<\/p>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">Well, those are the basics.\u00a0You mentioned things like &#8220;*x=**p++&#8221; being ugly and unreadable.\u00a0Well, yeah, but here is a diagram that may help:<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">       |----| here is a word in memory with initial value 0. \n0x5100 | 0  | no variable name<\/pre>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">       |----|\n0x5104 | 12 | here is a value, a word in memory. no variable name.\n       |----|\n0x5108 |5104| Here is an int pointer, pointing at the previous word.\n       |----|\n0x511c |5108| here is p, a pointer to int pointer.\n       |----|\n0x5120 |5100| here is x, a pointer. guess where it's pointing.\n       |----|<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">First let&#8217;s see what p and x were declared as:<\/p>\n<pre style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">int *x; \/* pointer to int *\/\nint **p; \/* pointer to pointer.<\/pre>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">The subordinate pointer is a pointer to int.*\/<\/p>\n<p>You should know now what &#8220;*x&#8221; means. It means, &#8220;the value of location 5100.&#8221;<\/p>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">And you know what &#8220;*p&#8221; means, &#8220;the value of location 5108&#8221;.\u00a0Now that value is another address! Okay, let&#8217;s dereference that\u00a0address: &#8220;**p&#8221; and we find (by the declaration) an int.<\/p>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">Now &#8220;*x = **p&#8221; looks like, &#8220;this int at 5100 gets the value of\u00a0that int at 5104.&#8221;<\/p>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">And what does &#8220;**p++&#8221; mean? Well, ++ binds tighter than *, so this\u00a0is equivalent to: *( *( p++ ) )<\/p>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\">Or, &#8220;pointer to pointer to int, and by the way, after we&#8217;re done,\u00a0p has been incremented. But we looked where it was pointing\u00a0before it got incremented, so we don&#8217;t care. Let the next statement\u00a0worry about it.&#8221;<\/p>\n<hr \/>\n<p style=\"-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;\"><span style=\"color: #333333; font-size: 1rem;\">This content is copyright 2012 by Bill Karwin. \u00a0I&#8217;ll share it under the terms of the Creative Commons License, Attribution-NonCommercial-ShareAlike 3.0 Unported.<\/span><\/p>\n<div>\n<div style=\"margin: 0px;\"><a href=\"http:\/\/creativecommons.org\/licenses\/by-nc-sa\/3.0\/\">http:\/\/creativecommons.org\/licenses\/by-nc-sa\/3.0\/<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>While I was in college, a friend of mine complained that he was confused while programming in C, struggling to learn the syntax for pointers. He gave the example of something like: *x=**p++ being ugly and unreadable, with too many operations layered on each other, making it hard to tell what was happening. \u00a0He said [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pawgV7-f","jetpack-related-posts":[],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":0,"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"wp:attachment":[{"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}