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.
what about for strings?
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]”
};
should be
char* strArray[5][4]
doesn’t it?
very helpful n easy to unterstand
thanks for helpfull information…
on array looping
a[i] = 0;
where it come from and how?
pls help
very helpful
thnx sir
very easy to unterstand
thnx
i want to know,how many dimensional array can be made?i mean one,two,three….and so on,….but up to which range?
@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!
explanation is very clear and understandable is easy
It was very helpful and is easy to understand.
Good one but it is basic information,
need more explanation in this Topic.
i understood array program thanks
thanks i got it……:)
what is an automatic array?
a[i][j] = 0;
what is it does?
i want to know how can we print arrays from a .txt input file??
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!
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.
How can sort the 2-dimensional array sorting(Bubble sort)..
thank’s for Good job…………
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
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
what is the advantage of having the datatype as char*