<?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: Ian Kamande</title>
    <description>The latest articles on DEV Community by Ian Kamande (@ian_kamande).</description>
    <link>https://dev.to/ian_kamande</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%2F665264%2F4ddc9a96-a2e6-4073-85f5-289e31936aa5.JPG</url>
      <title>DEV Community: Ian Kamande</title>
      <link>https://dev.to/ian_kamande</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ian_kamande"/>
    <language>en</language>
    <item>
      <title>What I learnt about arrays and pointers in C (As a refresher)</title>
      <dc:creator>Ian Kamande</dc:creator>
      <pubDate>Tue, 28 Oct 2025 04:19:10 +0000</pubDate>
      <link>https://dev.to/ian_kamande/what-i-learnt-about-arrays-and-pointers-in-c-as-a-refresher-2419</link>
      <guid>https://dev.to/ian_kamande/what-i-learnt-about-arrays-and-pointers-in-c-as-a-refresher-2419</guid>
      <description>&lt;h2&gt;
  
  
  Where it started
&lt;/h2&gt;

&lt;p&gt;I've been procrastinating about learning DSA for a while now but finally I started the journey. I chose C as the language I want to learn with because of its low level nature which i think would enforce a deeper understanding.&lt;/p&gt;

&lt;h2&gt;
  
  
  what are arrays and why
&lt;/h2&gt;

&lt;p&gt;In c, arrays are a collection of data of the same type. You could have an array of integers or an array of characters which is also known as a string.&lt;/p&gt;

&lt;p&gt;Arrays are important in the sense that they help organize data and ease the process of manipulating data. Take for instance you wanted to store the marks for 10 students and compute the average eventually. &lt;br&gt;
&lt;strong&gt;Option A:&lt;/strong&gt; You could do it is you could have ten different integer variables for each student's marks then calculate the average for all of them.&lt;br&gt;
&lt;strong&gt;Option B:&lt;/strong&gt; Since all marks are of type integer you could store them in one variable (a marks array) and calculate the average from there. This is how you would do that.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;int marks[10]  = {10, 9, 3, 4, 7, 9, 6, 8, 2, 5}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Clearly option B is more organized, easy to manipulate and memory efficient in comparison to A.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So how do you declare an array?&lt;/strong&gt;&lt;br&gt;
It's good to understand the difference between declaration, definition and initialization when it comes to variables in c.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Declaration: simply announcing a variables name to the compiler so it knows it exists.&lt;/li&gt;
&lt;li&gt;Definition: when memory for a defined variable is allocated.&lt;/li&gt;
&lt;li&gt;Initialization: when a value is assigned to a given variable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most of the time but not always declaration and definition occur at the same time together. For example we want to declare an array &lt;code&gt;marks&lt;/code&gt; which will hold integers, so we say: &lt;br&gt;
&lt;code&gt;int marks&lt;/code&gt; but this is incomplete since we're defining an array and not a simple integer. So to complete the array declaration we also need to define its length, i.e.&lt;br&gt;
&lt;code&gt;int marks[10]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After defining our variable we can initialize it like we had done earlier before:&lt;br&gt;
&lt;code&gt;int marks[10]  = {10, 9, 3, 4, 7, 9, 6, 8, 2, 5}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Another thing to note is once an array is declared in c its type cannot be changed. This is because C is a statically typed language. This means that the types of variables are declared explicitly and then checked during compilation(compile time) i.e. before the application runs.&lt;/p&gt;

&lt;p&gt;Other language types include dynamically typed languages which means that variable types are checked during runtime. No explicit type checks are enforced when the developer is writing  the code. A good example of this is JavaScript.&lt;/p&gt;

&lt;p&gt;Lastly there are hybrids of both statically and dynamically typed languages. In essence this means that as a developer you have the option to assign types before run/compile time or the language will assign types  for you dynamically during run/compile time.&lt;/p&gt;

&lt;p&gt;Now that we have defined our array how can we access it..&lt;br&gt;
Let's try access the items in the array and compute the average.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pointers&lt;/strong&gt;&lt;br&gt;
This is where pointers come in. You can think of pointers as memory holders. They basically hold the memory address of a variable that has been declared. The size of a pointer is actually 8 bytes in a 64 bit machine and 4 bytes in a 32 bit machine, (the latter is less common nowadays). The type of the pointer during declaration should be similar to the type of the variable you want to point to. So if i declare an integer variable age whose value is 19, and an integer pointer p and assign it the address of age, p will always show us where age lives in memory and can help us access and use age in our computation. Size of pointers is consistent and does not change once declared.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int age = 19;
int *p;

// assigning the address of age to p
p = &amp;amp;age;

// we can change the value of age to 20 using our pointer
*p =  20; // this is called dereferencing

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's a lot to pointers which you can check out later but this is enough for now.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; The (*) operator is called a dereferencing operator and helps us access and manipulate the value of the variable that out pointer is pointing to.&lt;/li&gt;
&lt;li&gt;The (&amp;amp;) operator is called the address of operator and it helps us access the memory address of the variable that our pointer points to.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;now back to the array&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One thing to note about the nature of arrays in memory(RAM) is that they are stored in a contiguous nature, this means their memory addresses follow each other one after the other. If we have &lt;code&gt;int arr[] = {10, 20, 15}&lt;/code&gt; and the memory address of &lt;code&gt;10&lt;/code&gt; is &lt;code&gt;0x01&lt;/code&gt; that would mean that the memory addresses of &lt;code&gt;20&lt;/code&gt; &amp;amp; b&lt;code&gt;15&lt;/code&gt; would be &lt;code&gt;0x02&lt;/code&gt; &amp;amp; &lt;code&gt;0x03&lt;/code&gt; respectively.&lt;/p&gt;

&lt;p&gt;Using pointers, if we declare a pointer that pointing to the array &lt;code&gt;arr&lt;/code&gt; i.e. &lt;code&gt;int p = &amp;amp;arr;&lt;/code&gt; dereferencing p will give us the value of the first item in the array, so, &lt;code&gt;*p&lt;/code&gt; is going to be &lt;code&gt;10&lt;/code&gt;. To get the value of the second item in the array we need to do some &lt;em&gt;pointer arithmetic&lt;/em&gt;, which basically is &lt;code&gt;*(p + 1)&lt;/code&gt; and to get the third value we would do something like &lt;code&gt;*(p + 2)&lt;/code&gt; you see the pattern.&lt;/p&gt;

&lt;p&gt;Knowing all this, we can now calculate the average mark for 5 students. Check out the code below,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;

int main()
{
        int marks[5]  = {9, 11, 10, 14, 6};
        int *marksPtr = marks;
        int sum = 0;
        int n  =  sizeof(marks) / sizeof(marks[0]);

        int avg;

        for (int i = 0; i &amp;lt; n; i++)
        {
                printf("We are currently at index %d and item is: %d \n", i, *(marksPtr + i));
                sum += *(marksPtr + i);
                // marksPtr++;
        }
        printf("Sum after looping: %d \n", sum);

        // so to get the average we do sum/size
        avg = sum / n;

        printf("Average is: %d\n", avg);

        return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please compile, run and experiment with the code to get a deeper understanding. &lt;/p&gt;

&lt;p&gt;That said, this is just the start of my DSA journey in C and Python(in future). Next, I’ll be exploring pointer arithmetic and dynamic memory allocation — one step at a time. Share and follow for more.&lt;/p&gt;

</description>
      <category>c</category>
      <category>arrays</category>
      <category>pointers</category>
      <category>lowlevelprogramming</category>
    </item>
  </channel>
</rss>
