One Dimensional And Two Dimensional Array In C
PrevNext
Write C code to dynamically allocate one, two and three dimensional arrays (using malloc) Its pretty simple to do this in the C language if you know how to use C pointers. Here are some example C code snipptes. One dimensional array. I have a 49 space one dimensional array declared as int boardArray 49; and I also have a two dimensional 7x7 array declared as int boardArrayTwo 77' I am trying to use nested for loops to throw the one dimensional array into the two dimensional array here is the code I am using to test it. An Array having more than one dimension is called Multi Dimensional array in C. In our previous article, we discussed Two Dimensional Array, which is the simplest form of C Multi Dimensional Array. In C Programming Language, by placing n number of brackets , we can declare n-dimensional array where n is dimension number.
C Array is a collection of variables belongings to the same data type. You can store group of data of same data type in an array.

- Array might be belonging to any of the data types
- Array size must be a constant value.
- Always, Contiguous (adjacent) memory locations are used to store array elements in memory.
- It is a best practice to initialize an array to zero or null while declaring, if we don’t assign any values to array.
Example for C Arrays:
- int a[10]; // integer array
- char b[10]; // character array i.e. string

Types of C arrays:
There are 2 types of C arrays. They are,

- One dimensional array
- Multi dimensional array
- Two dimensional array
- Three dimensional array
- four dimensional array etc…
1. One dimensional array in C:
Syntax : readonly='>#include<stdio.h> int main() { int i; int arr[5] = {10,20,30,40,50}; // declaring and Initializing array in C //To initialize all array elements to 0, use int arr[5]={0}; /* Above array can be initialized as below also arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50; */ for (i=0;i<5;i++) { // Accessing each variable printf('value of arr[%d] is %d n', i, arr[i]); } }
2 4 6 8 10 12 14 16 18 20 22 | { intarr[5]={10,20,30,40,50}; // declaring and Initializing array in C //To initialize all array elements to 0, use int arr[5]={0}; arr[0] = 10; arr[2] = 30; arr[4] = 50; */ for(i=0;i<5;i++) // Accessing each variable } } |
Output:
value of arr[1] is 20 value of arr[3] is 40 |
2. Two dimensional array in C:
- Two dimensional array is nothing but array of array.
- syntax : data_type array_name[num_of_rows][num_of_column];
Array declaration, initialization and accessing | |
Array declaration syntax: data_type arr_name [num_of_rows][num_of_column];Array initialization syntax: data_type arr_name[2][2] = {{0,0},{0,1},{1,0},{1,1}};Array accessing syntax: arr_name[index]; | Integer array example: int arr[2][2]; arr [0] [0] = 1; |
One Dimensional And Two Dimensional Array In C++
Example program for two dimensional array in C:
