{"id":31140,"date":"2023-01-19T16:30:25","date_gmt":"2023-01-19T11:00:25","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/"},"modified":"2023-01-19T16:30:25","modified_gmt":"2023-01-19T11:00:25","slug":"solved-need-help-in-clarifying-some-memory-references-pointers-in-c","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/","title":{"rendered":"[Solved] Need help in clarifying some memory references\/pointers in C"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-26539876\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"26539876\" data-parentid=\"26538662\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Let&#8217;s see if I can take a go at this.  First off, I recommend getting a simple, free c compiler to try these examples out.  Most of these examples can be tested that way.  <\/p>\n<pre><code>  1.   var -&gt; a[var -&gt; count]     vs   &amp;var -&gt; a[var -&gt; count]\n<\/code><\/pre>\n<p>Beware that &#8216;var&#8217; is a pointer to a data structure.  We can assume that the structure at the very least has two elements: an array of &#8216;a&#8217;, and a value &#8216;count&#8217;.  With this in mind &#8220;var-&gt;a[var-&gt;count]&#8221; represents the VALUE of the &#8216;count'(plus 1) element of the &#8216;a&#8217; array within the structure pointed to by var.  I say the &#8216;count&#8217; (+1) value because the first element of the array is a[0], as you know.  The term &#8220;&amp;var-&gt;a[var-&gt;count]&#8221; represents the ADDRESS of the &#8216;count'(plus 1) element of the &#8216;a&#8217; array within the structure pointed to by var.  <\/p>\n<pre><code>  2.  p = &amp;x[i]\n<\/code><\/pre>\n<p>This is setting p to the ADDRESS of the ith value of the &#8216;x&#8217; array.                  &#8216;&amp;varname&#8217; should be interpreted as (ADDRESS of varname).<\/p>\n<pre><code>      p = a[i]   \n<\/code><\/pre>\n<p>Here &#8216;p&#8217; is set to the VALUE of the ith value of the &#8216;a&#8217; array.<\/p>\n<pre><code>      &amp;p = &amp;a[i]` \n<\/code><\/pre>\n<p>This does not work.  First the back-tick means nothing.  Also, the attempt is to set the address of the pre-defined variable &#8216;p&#8217; to a new value.  &#8216;p&#8217; itself is a variable that can hold many different values, but the ADDRESS of p is NOT variable.  In case you&#8217;re<br \/>\nwondering, the expression should be read as &#8220;the address of p is equal to the address of a[i]&#8221;.<\/p>\n<pre><code>  3.   var-&gt;*mem     \n<\/code><\/pre>\n<p>Looks terribly wrong.  I would say this is an attempt to write&#8230;<\/p>\n<pre><code>       *var-&gt;mem      \n<\/code><\/pre>\n<p>Which is a the VALUE of some variable pointed to by &#8216;var-&gt;mem&#8217;.  In this case, &#8216;var&#8217; is a pointer to a structure, which contains the pointer &#8216;mem&#8217;.  In code it might look like this:<\/p>\n<pre><code>               int j;\n\n               struct example\n               {\n                  int *mem;  \/\/ mem is an integer pointer.\n               } x;\n\n               struct example *var;\n\n               j = 3;  \/\/ some value.\n\n               x.mem = &amp;j; \/\/ mem is set to point to j mem=ADDRESS of j;\n\n               var = &amp;x;   \/\/ var is pointing to the structure 'x'\n\n               printf(\"%d\\n\",*var-&gt;mem);  \/\/ should print \"3\"\n<\/code><\/pre>\n<p>This next example is the same mistake as the first example.<\/p>\n<pre><code>       var -&gt; *(mem) = 2;  \n<\/code><\/pre>\n<p>The parenthesis do nothing for you. Good luck getting this to compile.  That said, there is nothing stopping you from doing this:<\/p>\n<pre><code>       *var-&gt;mem = 2;\n<\/code><\/pre>\n<p>but in the above code example, that&#8217;s the same as &#8216;j=2&#8217;<\/p>\n<pre><code>    *(*var)-&gt;mem     \n<\/code><\/pre>\n<p>This example is really tricky. It reads as the value of the value pointed to by var, which itself is a pointer to a structure containing &#8216;mem&#8217;.  This implies that var would be declared as  <\/p>\n<pre><code>                struct ***var; \n<\/code><\/pre>\n<p>var is thus a pointer to a pointer to a pointer.  I created a code snippet which compiles and illustrates how this could be used, but I don&#8217;t recommend actually using this level of indirection in your own code, especially if there&#8217;s any chance someone will read it.  If you do, prepare to be hunted down and burnt at the stake like a witch.<\/p>\n<pre class=\"lang-c prettyprint-override\"><code>#include &lt;stdio.h&gt;\n\nstruct lkj\n{\n    int *mem;\n};\n\nint main(void)\n{\n    int j;\n\n    struct lkj bob;\n\n    struct lkj ***var,**floyd,*sam;\n\n    j = 3;\n    bob.mem = &amp;j;    \n    sam = &amp;bob;\n    floyd = &amp;sam;\n    var = &amp;floyd;\n\n    printf(\"bob.mem = 0x08%lX *bob.mem=%d j=%d\\n\",\n                (unsigned int)bob.mem,\n                *bob.mem,j);\n\n    printf(\"&amp;bob = 0x08%lX\\n\",(unsigned int)&amp;bob);\n\n    printf(\"sam = 0x08%lX &amp;sam=0x%08lX\\n\",\n            (unsigned int)sam,(unsigned int)&amp;sam);\n    printf(\"floyd = 0x08%lX &amp;floyd=0x%08lX\\n\",\n            (unsigned int)floyd,(unsigned int)&amp;floyd);\n\n    printf(\"var = 0x%08lX *(*var) = 0x%08lX\\n\",\n            (unsigned int)var,(unsigned int)*(*var));\n\n    printf(\"(*(*var))-&gt;mem = 0x%08lX *(*(*var))-&gt;mem=%d\\n\",\n            (unsigned int)(*(*var))-&gt;mem,\n            *(*(*var))-&gt;mem);\n\n    printf(\"(**var)-&gt;mem = 0x%08lX *(**var)-&gt;mem=%d\\n\",\n            (unsigned int)(**var)-&gt;mem,\n            *(**var)-&gt;mem);\n\n    return 0;\n}\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Need help in clarifying some memory references\/pointers in C <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Let&#8217;s see if I can take a go at this. First off, I recommend getting a simple, free c compiler to try these examples out. Most of these examples can be tested that way. 1. var -&gt; a[var -&gt; count] vs &amp;var -&gt; a[var -&gt; count] Beware that &#8216;var&#8217; is a pointer to a &#8230; <a title=\"[Solved] Need help in clarifying some memory references\/pointers in C\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/\" aria-label=\"More on [Solved] Need help in clarifying some memory references\/pointers in C\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324],"class_list":["post-31140","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Need help in clarifying some memory references\/pointers in C - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Need help in clarifying some memory references\/pointers in C - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Let&#8217;s see if I can take a go at this. First off, I recommend getting a simple, free c compiler to try these examples out. Most of these examples can be tested that way. 1. var -&gt; a[var -&gt; count] vs &amp;var -&gt; a[var -&gt; count] Beware that &#8216;var&#8217; is a pointer to a ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-19T11:00:25+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Need help in clarifying some memory references\/pointers in C\",\"datePublished\":\"2023-01-19T11:00:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/\"},\"wordCount\":465,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/\",\"name\":\"[Solved] Need help in clarifying some memory references\/pointers in C - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-01-19T11:00:25+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Need help in clarifying some memory references\/pointers in C\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Need help in clarifying some memory references\/pointers in C - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Need help in clarifying some memory references\/pointers in C - JassWeb","og_description":"[ad_1] Let&#8217;s see if I can take a go at this. First off, I recommend getting a simple, free c compiler to try these examples out. Most of these examples can be tested that way. 1. var -&gt; a[var -&gt; count] vs &amp;var -&gt; a[var -&gt; count] Beware that &#8216;var&#8217; is a pointer to a ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/","og_site_name":"JassWeb","article_published_time":"2023-01-19T11:00:25+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Need help in clarifying some memory references\/pointers in C","datePublished":"2023-01-19T11:00:25+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/"},"wordCount":465,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/","url":"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/","name":"[Solved] Need help in clarifying some memory references\/pointers in C - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-19T11:00:25+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-need-help-in-clarifying-some-memory-references-pointers-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Need help in clarifying some memory references\/pointers in C"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/31140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=31140"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/31140\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=31140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=31140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=31140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}