<?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/"
             xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
             xmlns:admin="http://webns.net/mvcb/"
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
             xmlns:content="http://purl.org/rss/1.0/modules/content/">
        <channel>
            <title>
									What is an array in C - C / C++				            </title>
            <link>https://www.hacktheforum.com/c-c/what-is-an-array-in-c/</link>
            <description>Hack The Forum Discussion Board</description>
            <language>en</language>
            <lastBuildDate>Mon, 11 May 2026 04:16:07 +0000</lastBuildDate>
            <generator>wpForo</generator>
            <ttl>60</ttl>
							                    <item>
                        <title>What is an array in C</title>
                        <link>https://www.hacktheforum.com/c-c/what-is-an-array-in-c/#post-892</link>
                        <pubDate>Sun, 16 Mar 2025 15:49:07 +0000</pubDate>
                        <description><![CDATA[An array is a collection of elements, all of the same type, stored in contiguous memory locations. It allows you to store multiple values of the same data type under a single variable name. ...]]></description>
                        <content:encoded><![CDATA[<p data-start="0" data-end="312">An <strong data-start="9" data-end="18">array</strong> is a collection of elements, all of the same type, stored in contiguous memory locations. It allows you to store multiple values of the same data type under a single variable name. Arrays are used when you need to store a fixed number of values of the same type and access them using an index.</p>
<h3 data-start="314" data-end="353">Key Characteristics of Arrays in C:</h3>
<ol data-start="354" data-end="708">
<li data-start="354" data-end="478"><strong data-start="357" data-end="371">Fixed Size</strong>: The size of the array must be defined at compile time, and it cannot be changed during program execution.</li>
<li data-start="479" data-end="596"><strong data-start="482" data-end="505">Zero-based Indexing</strong>: Array indices start from 0. For an array of size <code data-start="556" data-end="559">n</code>, the valid indices are <code data-start="583" data-end="586">0</code> to <code data-start="590" data-end="595">n-1</code>.</li>
<li data-start="597" data-end="708"><strong data-start="600" data-end="615">Homogeneous</strong>: All elements in an array must be of the same data type, such as all integers or all floats.</li>
</ol>
<h3 data-start="710" data-end="744">Syntax for Declaring an Array:</h3>
<div class="contain-inline-size rounded-md border- border-token-border-medium relative bg-token-sidebar-surface-primary">
<div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-">
<pre contenteditable="false">data_type array_name;</pre>
</div>
</div>
<ul data-start="788" data-end="962">
<li data-start="788" data-end="855"><strong data-start="790" data-end="805"><code data-start="792" data-end="803">data_type</code></strong>: The type of data (e.g., <code data-start="831" data-end="836">int</code>, <code data-start="838" data-end="845">float</code>, <code data-start="847" data-end="853">char</code>).</li>
<li data-start="856" data-end="898"><strong data-start="858" data-end="874"><code data-start="860" data-end="872">array_name</code></strong>: The name of the array.</li>
<li data-start="899" data-end="962"><strong data-start="901" data-end="917"><code data-start="903" data-end="915">array_size</code></strong>: The number of elements the array will hold.</li>
</ul>
<h3 data-start="964" data-end="976">Example:</h3>
<div class="contain-inline-size rounded-md border- border-token-border-medium relative bg-token-sidebar-surface-primary">
<div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-">
<pre contenteditable="false">int numbers; // Declares an array of 5 integers</pre>
</div>
</div>
<p data-start="1038" data-end="1149">Here, <code data-start="1044" data-end="1053">numbers</code> is an array that can store 5 integers. You can access these integers using indices from 0 to 4.</p>
<h3 data-start="1151" data-end="1177">Initializing an Array:</h3>
<p data-start="1178" data-end="1233">You can initialize an array at the time of declaration.</p>
<h4 data-start="1235" data-end="1274">Example 1: Explicit Initialization</h4>
<div class="contain-inline-size rounded-md border- border-token-border-medium relative bg-token-sidebar-surface-primary">
<div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-">
<pre contenteditable="false">int numbers = {1, 2, 3, 4, 5};</pre>
</div>
</div>
<h4 data-start="1319" data-end="1390">Example 2: Implicit Initialization (Size Determined Automatically)</h4>
<div class="contain-inline-size rounded-md border- border-token-border-medium relative bg-token-sidebar-surface-primary">
<div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-">
<pre contenteditable="false">int numbers[] = {1, 2, 3, 4, 5}; // Compiler automatically sets size to 5</pre>
</div>
</div>
<h4 data-start="1476" data-end="1523">Example 3: Partially Initializing an Array</h4>
<div class="contain-inline-size rounded-md border- border-token-border-medium relative bg-token-sidebar-surface-primary">
<div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-">
<pre contenteditable="false">int numbers = {1, 2}; // The remaining elements will be initialized to 0</pre>
</div>
</div>
<h3 data-start="1611" data-end="1640">Accessing Array Elements:</h3>
<p data-start="1641" data-end="1747">You can access or modify elements in the array by using the index of the element. The index starts at <code data-start="1743" data-end="1746">0</code>.</p>
<h4 data-start="1749" data-end="1762">Example:</h4>
<div class="contain-inline-size rounded-md border- border-token-border-medium relative bg-token-sidebar-surface-primary">
<div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-">
<pre contenteditable="false">int numbers = {1, 2, 3, 4, 5}; printf("%d", numbers); // Output will be 3, as array indices start at 0 numbers = 10; // Modifies the third element (index 2) printf("%d", numbers); // Output will now be 10</pre>
</div>
</div>
<h3 data-start="2002" data-end="2031">Multi-dimensional Arrays:</h3>
<p data-start="2032" data-end="2214">C also allows multi-dimensional arrays, where you can create arrays of arrays. The most common example is a <strong data-start="2140" data-end="2152">2D array</strong> (array of arrays), often used to represent matrices or grids.</p>
<h4 data-start="2216" data-end="2241">Example of 2D Array:</h4>
<div class="contain-inline-size rounded-md border- border-token-border-medium relative bg-token-sidebar-surface-primary">
<div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-">
<pre contenteditable="false">int matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };</pre>
</div>
</div>
<p data-start="2319" data-end="2445">Here, <code data-start="2325" data-end="2333">matrix</code> is a 2D array with 3 rows and 3 columns. To access an element, you need to specify both row and column indices:</p>
<div class="contain-inline-size rounded-md border- border-token-border-medium relative bg-token-sidebar-surface-primary">
<div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-">
<pre contenteditable="false">printf("%d", matrix); // Output will be 6 (second row, third column)</pre>
</div>
</div>
<h3 data-start="2532" data-end="2552">Important Notes:</h3>
<ul data-start="2553" data-end="2885">
<li data-start="2553" data-end="2718"><strong data-start="2555" data-end="2571">Array Bounds</strong>: Accessing an array with an index outside of its defined range results in <strong data-start="2646" data-end="2668">undefined behavior</strong>, which can lead to errors or unexpected results.</li>
<li data-start="2719" data-end="2885"><strong data-start="2721" data-end="2735">Array Size</strong>: In C, the size of an array is fixed once defined, and this cannot be changed dynamically (unless using dynamic memory allocation, e.g., <code data-start="2873" data-end="2883">malloc()</code>).</li>
</ul>]]></content:encoded>
						                            <category domain="https://www.hacktheforum.com/c-c/">C / C++</category>                        <dc:creator>kajal</dc:creator>
                        <guid isPermaLink="true">https://www.hacktheforum.com/c-c/what-is-an-array-in-c/#post-892</guid>
                    </item>
							        </channel>
        </rss>
		