{"id":1731,"date":"2008-05-28T11:18:32","date_gmt":"2008-05-28T08:18:32","guid":{"rendered":"http:\/\/mummila.net\/nuudelisoppa\/?p=1731"},"modified":"2012-05-11T18:13:58","modified_gmt":"2012-05-11T15:13:58","slug":"banging-my-head-against-c-pointer-dereference","status":"publish","type":"post","link":"https:\/\/mummila.net\/nuudelisoppa\/2008\/05\/28\/banging-my-head-against-c-pointer-dereference\/","title":{"rendered":"Banging my head against C pointer dereference"},"content":{"rendered":"<p>The pointers always get me when I try my hands at C. Now, I&#8217;m not dumber than most people, and the basic idea is pretty clear to me. But what trips my brain each time is the notation of declaration.<\/p>\n<p>It&#8217;s not always illogical. After all, as K&amp;C points out, <q><a href=\"http:\/\/www.cs.utah.edu\/~phister\/K_n_R\/chapter5.html#s5.1\"><code>int *ip<\/code> is intended as a mnemonic<\/a>; it says that the expression <code>*ip<\/code> is an <code>int<\/code><\/q>. Okay, that seems innocent enough, but in fact it&#8217;s a two-edged sword. What about when you&#8217;re declaring a pointer as a parameter for a function, then calling the function? Compare these two short programs below:<\/p>\n<table>\n<tbody>\n<tr>\n<th>\nListing 1.\n<\/th>\n<th>\nListing 2.\n<\/th>\n<\/tr>\n<tr>\n<td>\n<pre><code>int foo(int i);\r\nint main() {\r\n int j = 1;\r\n foo(j);\r\n return 0;\r\n}\r\n\r\nint foo(int i) {\r\n  \/* do something with i *\/\r\n  return 0;\r\n}<\/code><\/pre>\n<\/td>\n<td>\n<pre><code>int foo(int *ip);\r\nint main() {\r\n  int j = 2;\r\n  foo(&amp;j);\r\n  return 0;\r\n}\r\n\r\nint foo(int *ip) {\r\n  \/* do something with (*)ip *\/\r\n  return 0;\r\n}<\/code><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>For the first program, it&#8217;s easy to see that the parameter of <code>foo()<\/code>, <code>i<\/code>, is assigned the contents of <code>j<\/code> from <code>main()<\/code>: you&#8217;re in effect saying:<\/p>\n<ol>\n<li>Let <code>foo()<\/code> have one parameter, <code>i<\/code>, of type <code>int<\/code>.<\/li>\n<li>From <code>main()<\/code>, assign the value of <code>j<\/code>, also of type <code>int<\/code>, to <code>i<\/code>.<\/li>\n<\/ol>\n<p>But for the second one, <code>foo()<\/code> has to be called with an <em>address<\/em> (a pointer), and yet the declaration, with the mnemonic notation in place, seems to call for an <code>int<\/code>:<\/p>\n<ol>\n<li>Let <code>foo()<\/code> have one parameter, <code>*ip<\/code>, of type <code>int<\/code>.<\/li>\n<li>From <code>main()<\/code>, assign the value of <code>&amp;j<\/code>, of type&#8230; uhh&#8230; <em>pointer<\/em>, to&#8230; uhh&#8230; ???<\/li>\n<\/ol>\n<p>This difficulty for me to grasp what type of a parameter <code>foo()<\/code> should be called with is due to the mnemonic notation in <code>int *ip<\/code>: when you have an <code>int &lt;something&gt;<\/code> as a parameter declaration, you expect to assign the parameter something that is of type <code>int<\/code> \u2014 not something that is a <em>pointer<\/em> to an <code>int<\/code>.<\/p>\n<p>Now, if I give up the mnemonic and instead write the declaration as <code>int* ip<\/code>, the code translates to natural language quite fluidly:<\/p>\n<table>\n<tbody>\n<tr>\n<th>Listing 3.<\/th>\n<\/tr>\n<tr>\n<td>\n<pre><code>int foo(int* ip);\r\nint main() {\r\n  int j = 2;\r\n  foo(&amp;j);\r\n  return 0;\r\n}\r\n\r\nint foo(int* ip) {\r\n  \/* do something with (*)ip *\/\r\n  return 0;\r\n}<\/code><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ol>\n<li>Let <code>foo()<\/code> have one parameter, <code>ip<\/code>, which is <em>a pointer to an <code>int<\/code><\/em>.<\/li>\n<li>From <code>main()<\/code>, assign the value of <code>&amp;j<\/code>, also a <em>pointer to an <code>int<\/code><\/em>, to <code>ip<\/code>.<\/li>\n<\/ol>\n<p>Of course, this notation opens a whole new can of worms. Consider the following declaration:<\/p>\n<blockquote>\n<pre><code>int* i, j;<\/code><\/pre>\n<\/blockquote>\n<p>Is <code>j<\/code> a pointer now, or just an <code>int<\/code>? It&#8217;s way too <a href=\"https:\/\/secure.wikimedia.org\/wikipedia\/en\/wiki\/C_variable_types_and_declarations#Pointer_types\">open to misinterpretation<\/a> to make this notation commendable despite the previous advantage in readability.<\/p>\n<p>So I&#8217;m better off sticking to the mnemonic notation, and just trying to get my head around it. For that, I&#8217;m still in desperate need for an easy translation of such code into natural language.<\/p>\n<p>Maybe the mnemonic is the root of the problem. It&#8217;s a really bad mnemonic because it almost always works, but then there&#8217;s (at least) this one case, where you&#8217;re better off not remembering it, because it screws up your logic if you do. So I should think of <code>int *ip<\/code> as <q>ip is a pointer to an <code>int<\/code><\/q> and just forget about the fact that it resembles a declaration of an <code>int<\/code> called <code>*ip<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The pointers always get me when I try my hands at C. Now, I&#8217;m not dumber than most people, and the basic idea is pretty clear to me. But what trips my brain each time is the notation of declaration. It&#8217;s not always illogical. After all, as K&amp;C points out, int *ip is intended as [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1731","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/posts\/1731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/comments?post=1731"}],"version-history":[{"count":3,"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/posts\/1731\/revisions"}],"predecessor-version":[{"id":3350,"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/posts\/1731\/revisions\/3350"}],"wp:attachment":[{"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/media?parent=1731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/categories?post=1731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/tags?post=1731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}