Apr 09, 2020

C programming array tutorial

In a previous article we discussed how we can declare and use variables in C. We know a variable is a memory space for hold some data. Think that there are 12 variables like month_1, month_2, month_3...., month_12 etc. Each of these variable are integer type and hold a number. What if we can make a one container to hold all of these 12 variables. That will simplify our life. Also we can write clean codes. The concept of array is much similar to this. We make a single variable to store multiple variable data. But there should be a way to access, modify and store each variable's data. Let's talk more about this array concept.

Hear is how we can declare an empty array.

int numbers[5];

This code line build a array with five elements. That means this is equal to five single integer variables. Since our array have five cells it can hold maximum five integers. An array has a index to access it's elements. We can use it to access, enter or modify it's values. The type of this array is int . So all elements should be integers.

Let's add some data to the newly created array.

numbers[0] = 254;
numbers[1] = 1;
numbers[2] = 0;
numbers[3] = 8;
numbers[4] = 4;

This is very similar to assigning a value to integer variable. But there are two things to note. The array's starting index is 0 and it ends with 4. So if an array has n elements it's range is 0 to n-1. In the following image you can see a memory layout of our array.

Our array's first element is 254. (fe in hexadecimal) is located at 0xbffffcf4 We can access it with numbers[0]. Second element numbers[1] is far four bytes from first element's address. Because one integer is four bytes long. So it's address should be 0xbffffcf8

Now we declared the array and stored data in it. Above two steps can be done in single line.

int numbers[5] = {254, 1, 0, 8, 4};

If we don't specify the size of array, compiler automatically create the array to match number of elements.

int numbers[] = {254, 1, 0, 8, 4};

Multidimensional arrays

A Multidimensional array have two or more dimensions. A two dimensions array is very helpful when we develop programs with coordinates like x and y axis. Also we can work with matrix using these multidimensional arrays. Following is the syntax to declare an empty multidimensional array.

int numbers[5][6];

int numbers[5][6][7];

First one is a two dimensional array and second one is a tree dimensional array.

int numbers[4][3] = { 6, 24, 5, 4, 5, 64, 7, 8, 9, 3, 10, 12 };
int numbers[4][3] = { 
					6, 24, 5, 4, 
					5, 64, 7, 8,
					9, 3, 10, 12 

Pointers and Arrays

In "Pointers in C" tutorial we discuses about pointers and there usage. So a pointer is always pointing to a memory address. Do you know that array name is also a pointer. Let's take above example.

int numbers[5];

Hear numbers is the name of the array. It points to starting memory address of the array. So as above example it should be .We can find array's first element there. As well as we can access array's first element by using numbers[0] . Since it is a variable we can access it's memory location by &numbers[0] . So both above methods give us same memory address. That means if "array_name" is name of a array, following is always true in C programming .

array_name == &array_name[0];

Aug 20
Assembly basic tutorial - add two numbers

Welcome guys, today another basic tutorial on assembly coding. Hear I'll explain you how we can add....

Mar 24
Array data structure

There are two primary ways to make an array in the memory. the first one is the most familiar one....

Nov 19
cookie consent banner javascript

Accordion to GDPR (General Data Protection Regulation) If we collect or save any kind of website....

Replying to 's comment Cancel reply
Thilan Danushka Dissanayaka

Thilan Dissanayaka

Hi, I'm Thilan from Srilanka. An undergraduate Engineering student of University of Ruhuna. I love to explorer things about CS, Hacking, Reverse engineering etc.