C Tutorial – Arrays and Multi-Dimensional Arrays

In this C programming language tutorial, we are going to talk about arrays.

An array lets you declare and work with a collection of values of the same type. Let’s say you want to declare four integers. With the knowledge from the last few tutorials you would do something like this:


int a , b , c , d;

What if you wanted to declare a thousand variables? That will take you a long time to type. This is where arrays come in handy. An easier way is to declare an array of four integers:


int a[4];

The four separate integers inside this array are accessed by an index. Each element can be accessed by using square brackets with the element number inside. All arrays start at element zero and will go to n-1. (In this case from 0 to 3). So if we want to fill each element you get something like this:


     int a[4];
     a[0] = 1;
     a[1] = 2;
     a[2] = 3;
     a[3] = 4;

If you want to use an element, for example for printing, you can do this:


printf("%d", a[1]);

Arrays and loops

One of the nice things about arrays is that you can use a loop to manipulate each element. When an array is declared, the values of each element are not set to zero automatically. In some cases you want to “initialize” the array (which means, setting every element to zero). This can be done like in the example above, but it is easier to use a loop. Here is an example:


#include<stdio.h>

int main()
{
	int a[4];
	int i;

	for ( i = 0; i < 4; i++ )
		a[i] = 0;

	for ( i = 0; i < 4; i++ )
		printf("a[%d] = %d\n", i , a[i]);

	return 0;
}

Multi-dimensional arrays

The arrays we have been using so far are called one-dimensional arrays.
Here is an example of an one-dimensional array:


int a[2];

0

1

1

2

Note: A one-dimensional array has one column of elements.

Two-dimensional arrays have rows and columns. See the example below:


int a[2][2];

0

1

0

1 2

1

4 5

Note: a[0][0] contains the value 1. a[0][1] contains the value 2. a[1][0] contains the value 4. a[1][1] contains the value 5.

So let’s look at an example that initialize a two-dimensional array and prints each element:


#include<stdio.h>

int main()
{
	int a[4][4], i , j;

	for (i = 0; i < 4; i++)
	{
		for ( j = 0; j < 4; j++)
		{
			a[i][j] = 0;
			printf("a[%d][%d] = %d \n", i, j, a[i][j]);
		}
	}
	return 0;
}

Note: As you can see, we use two “for loops” in the example above. One to access the rows and the other to access the columns.

A last word before we end this tutorial. The language C has no range checking, so if you index (choose an element) past the end of the array, it will not tell you. Instead the program will give you garbage data or it will crash.

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 26 responses to “C Tutorial – Arrays and Multi-Dimensional Arrays”

Why not let us know what you think by adding your own comment!

  1. via cals on September 15th, 2011:

    what about for strings?

  2. Csaba on November 8th, 2011:

    For Strings you just use like this (Example with initialisation):

    char* strArray[5][3] = {“[0].[0]”,”[0].[1]”,”[0].[2]”,”[0].[3]”
    ,”[1].[0]”,”[1].[1]”,”[1].[2]”,”[1].[3]”,
    ,”[2].[0]”,”[2].[1]”,”[2].[2]”,”[2].[3]”,
    ,”[3].[0]”,”[3].[1]”,”[3].[2]”,”[3].[3]”,
    ,”[4].[0]”,”[4].[1]”,”[4].[2]”,”[4].[3]”
    };

  3. cn on November 24th, 2011:

    should be
    char* strArray[5][4]
    doesn’t it?

  4. hiren on December 6th, 2011:

    very helpful n easy to unterstand

  5. binub m on December 31st, 2011:

    thanks for helpfull information…

  6. Gharib on February 4th, 2012:

    on array looping
    a[i] = 0;
    where it come from and how?
    pls help

  7. iola on July 25th, 2012:

    very helpful

  8. monu on October 3rd, 2012:

    thnx sir

  9. Sonu on October 10th, 2012:

    very easy to unterstand
    thnx

  10. amit on November 18th, 2012:

    i want to know,how many dimensional array can be made?i mean one,two,three….and so on,….but up to which range?

  11. admin on November 18th, 2012:

    @amit: I assume you mean the max number of indices (or dimensions). Multidimensional arrays are not limited, they can contain as many indices as needed. It is only restricted by memory usage, the amount of memory needed for an array can rapidly increase with each dimension you add. Take a look at the following array:

    char very-large-array[20][100][100][100][100];

    That are 2 billion chars in very-large-array and that is around 2 gigabyte of memory. Also you should notice the speed of your program, the larger the array the slower the usage (for instance in a loop).

    Hope this helps!

  12. ayub on February 1st, 2013:

    explanation is very clear and understandable is easy

  13. farheen on February 10th, 2013:

    It was very helpful and is easy to understand.

  14. Shivam Soni on May 25th, 2013:

    Good one but it is basic information,
    need more explanation in this Topic.

  15. deep on June 3rd, 2013:

    i understood array program thanks

  16. aijaz on June 13th, 2013:

    thanks i got it……:)

  17. SUNIL SINGH on August 4th, 2013:

    what is an automatic array?

  18. sujit on August 6th, 2013:

    a[i][j] = 0;
    what is it does?

  19. deepika on August 17th, 2013:

    i want to know how can we print arrays from a .txt input file??

  20. Abbas on September 8th, 2013:

    Automatic arrays are declared using the keyword “auto”. This keyword is one of the 4 storage classes ! Even if not declared as auto, a variable is set default as auto. Its scope is within the blocks. If not initialized , it takes garbage value and it is stored in the memory i.e, RAM .

    Gracia!

  21. ronk on October 8th, 2013:

    In the sample about multi-dimensional arrays it would be faster to grasp if you define the heading and row cells with a different colour to differentiate that from the content. Other wise simple and easy to grasp.

  22. Prasad on October 29th, 2013:

    How can sort the 2-dimensional array sorting(Bubble sort)..

  23. Abdul Hafeez on December 2nd, 2013:

    thank’s for Good job…………

  24. pramod on December 6th, 2013:

    how to find out integer value from the list of an array contain integer, float number.
    like – a[1,0.2,3,4.5]
    how to find integer values from this list

  25. abdul manan on December 7th, 2013:

    i want to make an array and store multiple digits like 1,2 and 3 in multi variables like 1 in a ,2 in b and 3 in c can you tell me how to do it

  26. sai bharathi on April 26th, 2014:

    what is the advantage of having the datatype as char*