<?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>Forem: Arghya Sahoo</title>
    <description>The latest articles on Forem by Arghya Sahoo (@arghyasahoo).</description>
    <link>https://forem.com/arghyasahoo</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%2F459173%2F9fcd1a77-958f-491a-927b-967ad20a69df.png</url>
      <title>Forem: Arghya Sahoo</title>
      <link>https://forem.com/arghyasahoo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/arghyasahoo"/>
    <language>en</language>
    <item>
      <title>Pointers</title>
      <dc:creator>Arghya Sahoo</dc:creator>
      <pubDate>Sat, 16 Apr 2022 12:06:49 +0000</pubDate>
      <link>https://forem.com/arghyasahoo/pointers-4i2h</link>
      <guid>https://forem.com/arghyasahoo/pointers-4i2h</guid>
      <description>&lt;p&gt;One of the fundamental concepts in computer programming is a &lt;strong&gt;variable&lt;/strong&gt;. If you are studying computer science, or programming in general, chances are you know what a variable is. In simple terms, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A variable is a container which holds a value&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A variable can hold values of certain datatypes, such as &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;char&lt;/code&gt;, &lt;code&gt;float&lt;/code&gt;, &lt;code&gt;bool&lt;/code&gt; etc. &lt;strong&gt;Pointers are nothing but a special kind of variable. Instead of storing a value, it stores address of a variable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In case all of this jargon didn't make any sense, we will dig deeper into this.&lt;/p&gt;

&lt;p&gt;A computer primarily consists of two types of storage, &lt;strong&gt;Primary Memory&lt;/strong&gt; and &lt;strong&gt;Secondary Memory&lt;/strong&gt;, whenever we execute a program, all of the variables declared in it gets stored in the Primary Memory a.k.a the Main Memory a.k.a the &lt;strong&gt;RAM&lt;/strong&gt;. Now the variables can be anywhere in the RAM, but how could we locate them?&lt;/p&gt;

&lt;p&gt;Lets say, you want to invite one of buddies to your house, but he has never visited your house before, then how would he find it? He will need an address. With an address he can locate your house exactly. But without one, he has to roam around the entire world until he finds your house. That doesn't sound like a fun task...does it?&lt;/p&gt;

&lt;p&gt;Similarly, every variable that is stored in the primary memory has a specific &lt;strong&gt;address assigned&lt;/strong&gt; to it. Different variables can not reside in the same address. Just like, the address of your house and your friends house can not be same unless you both live in the same house.&lt;/p&gt;

&lt;p&gt;Now you need to send your address to your friend. How would you do that? Let's say, your send a text message to your friend containing your address. your friend receives it, and comes to your place by locating the address and making his way to it.&lt;/p&gt;

&lt;p&gt;Let's retrace his steps, first he receives the text message, which reveals your address, then he locates the specific address to find your house. This is an example of a pointer. Just like the text message contained the address of your house, a pointer contains the address of a variable. &lt;strong&gt;It does not contain the actual value of the variable, rather an address where to find it&lt;/strong&gt;. Imagine if your text message contained your entire house in it. As the text message contained a reference to your actual house, a pointer contains a reference to an actual variable. Dereferencing the pointer reveals the value of the variable it is referencing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo04vryu5eqkkied46vjd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo04vryu5eqkkied46vjd.jpg" alt="Meme"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's talk CODE 👨‍💻
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;In C Language, a pointer is declared as &lt;code&gt;*&amp;lt;pointer_var_name&amp;gt;&lt;/code&gt; like &lt;code&gt;int *p;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The datatype of a pointer is same as the datatype of the variable it is storing. If a pointer referencing a variable which is storing integer value, the pointer will be an integer pointer, if the variable is storing a character, the pointer will be called a character pointer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&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;5&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;ip&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;// '&amp;amp;' is called the "address of" operator, and the '*' is known as "dereference" operator Here, the address of a is fetched and stored inside ip&lt;/span&gt;

&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;cp&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;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's dig deeper into the code. For the first example, let the assume that &lt;code&gt;a&lt;/code&gt; is stored in memory location &lt;code&gt;100&lt;/code&gt;. &lt;code&gt;a&lt;/code&gt; has a value of &lt;code&gt;5&lt;/code&gt; and &lt;code&gt;ip&lt;/code&gt; contains the address of &lt;code&gt;a&lt;/code&gt;. So, technically the value of &lt;code&gt;ip&lt;/code&gt; will be 100. Dereferencing &lt;code&gt;ip&lt;/code&gt; will result in the value of &lt;code&gt;a&lt;/code&gt; which is &lt;code&gt;5&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8usxv0ylewjl4votgeno.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8usxv0ylewjl4votgeno.png" alt="Single Pointer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pointers can also hold address of another pointer. They are called &lt;strong&gt;double pointers&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&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;5&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;ip&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;// ip holds the address of a&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;pp&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;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// pp holds the address of p, which holds the address of a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, dereferencing &lt;code&gt;ip&lt;/code&gt; once gives the value of &lt;code&gt;a&lt;/code&gt;. As it directly points to &lt;code&gt;a&lt;/code&gt;, but in case of &lt;code&gt;pp&lt;/code&gt; to get the value of &lt;code&gt;a&lt;/code&gt; you need to dereference twice. As, &lt;code&gt;pp&lt;/code&gt; is holding the address of &lt;code&gt;ip&lt;/code&gt; and &lt;code&gt;ip&lt;/code&gt; is holding the address of &lt;code&gt;a&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl8lavi0k4jhezo51wovp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl8lavi0k4jhezo51wovp.png" alt="Double Pointer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This way, pointers can be chained together.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pointers are SLOW! 🐌
&lt;/h2&gt;

&lt;p&gt;Whenever you execute a program, all the variables declared in it is stored in that programs stack frame which resides in RAM. Now, during the calculations, CPU fetches variables from the RAM into its internal registers. This memory access is not the fastest thing in the world. This process takes up quite a few nanoseconds. So, in case of directly accessing a variable, CPU needs to fetch the variable only once from the memory and it gets its value. But in case of pointers, multiple trips are required to the RAM from CPU to get a value. In case of single pointers, first the address of the variable is fetched and then again, the value variable is fetched from that specific memory address. In case of double pointers, it makes three trips to the RAM before it gets the value of the variable. So, longer the chain, the more time it will take to fetch the variable.&lt;/p&gt;

&lt;p&gt;Let's take the previous scenario. Instead of providing direct address to your house, you text your friend the address of your local grocery store, there a man hands him the address to your nearest rail station, there someone gives him the address to your house, This will surely take longer time, instead if he was given direct address to your house.&lt;/p&gt;

&lt;p&gt;That's all folks, hope this article gives you "yet another" viewpoint towards pointers. Pointers are often misunderstood by a lot of newbie programmers. When broken down to its basics, they are really simple and easy to grasp.&lt;/p&gt;

</description>
      <category>c</category>
      <category>programming</category>
      <category>pointers</category>
    </item>
  </channel>
</rss>
