<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Arjav Patel</title>
    <description>The latest articles on DEV Community by Arjav Patel (@arjbholu).</description>
    <link>https://dev.to/arjbholu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F96724%2F442cd305-7bfc-48d1-8cf3-0bbce3484607.png</url>
      <title>DEV Community: Arjav Patel</title>
      <link>https://dev.to/arjbholu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arjbholu"/>
    <language>en</language>
    <item>
      <title>Understanding pointers in c through an Analogy</title>
      <dc:creator>Arjav Patel</dc:creator>
      <pubDate>Fri, 07 Feb 2020 10:34:18 +0000</pubDate>
      <link>https://dev.to/arjbholu/understanding-pointers-in-c-through-an-analogy-580e</link>
      <guid>https://dev.to/arjbholu/understanding-pointers-in-c-through-an-analogy-580e</guid>
      <description>&lt;p&gt;Scenario 1:&lt;/p&gt;

&lt;p&gt;Let's assume you have a locker A. The locker is open. You can view the contents of the locker and change it. &lt;/p&gt;

&lt;p&gt;Scenario 2:&lt;/p&gt;

&lt;p&gt;Let's assume you have a locker A and B. Locker A is closed and locker B is open. The key to locker A is there in locker B. You can use the key from locker B and see and change the contents of locker A.&lt;/p&gt;

&lt;p&gt;Let's try to make an analogy in the playgrounds of c++.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Declaring the variable&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// viewing the content of the variable&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// changing the contents of the variable&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Connect to scenario 1. Assume 'a' is locker and is open. You can view its content and change it.&lt;/p&gt;

&lt;p&gt;Now let's connect to scenario 2. But before that, let us get familiar with the syntax of the pointers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Means 'b' is a pointer that is of type int. Here we are declaring the pointer&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Means 'c' is a pointer that is of type float.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now let's dive a little deep and connect to scenario 2.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//1 declaring pointer but not assigning it anything&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//2 declaring the int variable and assigning it a value;&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//3 Scenario 1&lt;/span&gt;
    &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//4 assigning the pointer 'b' the location of 'a'.&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//5 viewing the contents of 'a' through 'b'.&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//6 changing the contents of 'a' through 'b'.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Heavy, huh?&lt;/p&gt;

&lt;p&gt;Let's break it down piece by piece.&lt;br&gt;
&lt;strong&gt;Line 1&lt;/strong&gt;: We are declaring pointer as mentioned in the previous code snippet. Piece of cake. No worries here.&lt;br&gt;
&lt;strong&gt;Line 2&lt;/strong&gt;: We are declaring and initializing the int variable. Good until here! &lt;br&gt;
&lt;strong&gt;Line 3&lt;/strong&gt;: Printing the int variable 'a' and connecting to scenario 1. Locker A is open and we can view the contents of the locker. We can also change the contents of the locker. 'a = 30', this will change the contents of 'a'.&lt;br&gt;
&lt;strong&gt;Line 4&lt;/strong&gt;: Here where the magic happens. What this statement means is you are giving the key to Locker A to Locker B. So now using the key from locker B, you can change and view the contents of Locker A. In lines 5 and 6 is what we do.&lt;br&gt;
&lt;strong&gt;Line 5&lt;/strong&gt;: View the contents of Locker A through key which is there in Locker B.&lt;br&gt;
&lt;strong&gt;Line 6&lt;/strong&gt;: Change the contents of Locker A through the key from B.&lt;/p&gt;

&lt;p&gt;That's what pointer is. Yes, believe me, that's it. Everything else about pointer rotates around this concept. &lt;/p&gt;

&lt;p&gt;In the next post, we will discuss why we use a pointer in the first place? Why make complex things when everything can be done through simple chunks? How pointers are used in array? How pointers are used in Linked List?&lt;/p&gt;

&lt;p&gt;Hugs and Bugs are most welcome. If you like the post, just hit one of those emoticons so that I know I am able to help you.  If not, please comment so that I can improve or clear your doubts!&lt;/p&gt;

&lt;p&gt;More links for your reference:&lt;br&gt;
&lt;a href="https://www.geeksforgeeks.org/pointers-c-examples/"&gt;https://www.geeksforgeeks.org/pointers-c-examples/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://beginnersbook.com/2017/08/cpp-pointers/"&gt;https://beginnersbook.com/2017/08/cpp-pointers/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://gist.github.com/ericandrewlewis/720c374c29bbafadedc9"&gt;https://gist.github.com/ericandrewlewis/720c374c29bbafadedc9&lt;/a&gt;&lt;/p&gt;

</description>
      <category>c</category>
      <category>pointers</category>
      <category>beginners</category>
      <category>coding</category>
    </item>
  </channel>
</rss>
