An array is the collection of similar data type all the elements is stored at the contiguous memory location. In c or c++ language we can create a static or dynamic array, the static array has the fixed size but the size of the dynamic array can be changed at run time. The dynamic array is created using the pointer and library functions (malloc, calloc) or new operator which are used to allocate the memory from the heap or free store.

The array has the important place in the programming language, it plays the important role, where you need to store the similar data in the large amount.

For example,suppose you have an application, take the user information and send it to the server. The user information could be anything like the name, age, address..etc.We have already know that in the programming language, we can store the data in memory using the variable. So before sending the user information to the server, we need to store the data in the application.

I am assuming the length of the “address” is 100 bytes, so to store the user Address we have to need hundred of character variables, such as Addr0, Addr1….Addr99.It’s very hard to access the 100 variable for the user address, it also increases the length of the code and reduced the readability.

Using the array we can resolve this issues, we just need to declare a character array whose size is 100. If the Addr is character array of size 100,we can use Addr[0], Addr[1],… Adrr[99] as the individual variable to store or retrieve the data. In the array, we can access the specific element using the array index.

Note: In array first element at the lowest address and last element at the highest address.



Declaration of array in c

In c/c++ language we can create single dimensional or multiple dimensional arrays. Declaration of the array is very simple, we need to write array name followed by the data type and square brackets [ ], square brackets contain the size of the array. The size of the array must be an integral constant expression.

Note: In C99, we can also use the variable for the size of the array but the value of the variable should be the integral positive number.

The general syntax of a single dimensional array in c

Data_Type  Array_Name [size];

General Syntax of multiple dimensional array in c

Data_Type  Array_Name  [size1] [size2] [size3]…. [sizen];

In the above declaration, data type should be valid and array name should follow the naming rules. The size of the array should be integer constant and greater than zero.

Let’s consider an example, to declare an array “acBuffer” that contains 10 character, use the below expression.

char acBuffer [10];

Note: Below declaration valid on C99

int ArraySize = 10;
char acBuffer [ArraySize];

Let’s consider below example program which is not valid in plain C.

Above program asked from the user for the array length. If the user enters a positive number then it creates an array whose size is equal to the entered number.



Initialization of array in c

There are a lot of ways to initialize the array, we can initialize the array using the single expression or we can explicitly assign the value to the array using the array index.

int aiData[10] = {0,1,2,3,55,6,7,8,8,9};

The value between the curly braces should be in the array boundary because there is no boundary checking in the array. If you want you can also omit the size of the array at the time of array initialization.

int aiData[ ] = {0,1,2,3,55,,8,9};

In the above expression, array size becomes equal to the number of the elements between the curly braces. One of the great features of the array, if you want to initialize the array using the zero (0), you need to write 0 in curly braces ones.

int aiData[20] = {0};

Note: Use the memset to clear all block of the character array, it provides the surety that every block of the array contains zero.

The below example shows how to use the memset with the array.

array in c

In above program, I am assigning 0 to each block of the character array. Generally, people make mistake when using the memset with integer array, so before using the memset must read about the memset.

We can also assign a single value to the array using the subscript and index. Let’s consider the below example.

int aiData[5] ={1,2,3,4,5};

array in c

If I write the below expression,

aiData[ 2 ] = 7;

The above statement assigns 7 to the 3rd element in the array.

array in c pdf

 All arrays have 0 as the index of their first element which is also called the base index and the last index of an array will be the total size of the array minus 1.



Designated Initializers

The C99 introduces a new mechanism to initialize the elements of the array. It allows you to initialize specific elements of the array in any sequence, you don’t need to initialize the array from the beginning.

In this method, if the size of the array is not given, then the largest initialized position become the size of the array and all uninitialized position initialized with 0.

Let’s consider an example

I am creating an integer array aiData, whose size is 50 and initializing the elements of the array using their index.

int aiData[20] = { 1, 2, 3, [15] = 40, 5, [13] = 80, [18] = 89 };

In this example, the first three elements are initialized to 1, 2, and 3 respectively. Then index 15 ( 16th element of the array) is initialized to 40 The next element ( 17th ) is initialized to 5. Then indexes 13, 18 ( 14th, 19th elements of the array ) is initialized to 80 and 89 respectively.

c99 designated initializer

Accessing the 1D array in c

In the above statement, I have described how we can access the array using the array subscripts. In below code using the for loop, I am assigning the value to the array of integers with the help of index and square brackets and again read back the stored value.

1d array in c

Note: One of the biggest drawback of the array there is no boundary checking by the compiler in c language.

Multidimensional array in c

In the beginning of the article, I have described syntax how to create a multidimensional array. The number of the square brackets decide the dimension of the array, 1D array have the one square brackets and 2D array have the two.. etc.

If we initialize the multidimensional array completely then we can omit the first square brackets subscript (index). For example, a 1D array does not require the size of the array if we initialized the array.

int aiData[] ={1,4,5,9,20};

Like this, we can remove the first subscript of the 2D array, in the case of the 2D array first subscript call row and second subscript called columns. So we can say that we can omit the row of the 2D array if we initialized the 2D array.

int aiData [ ][ 3 ] = { { 9, 6, 1 }, { 144, 70, 50 }, {10, 12, 78} };

Note: We can omit the row of the 2D array in c.

In C/C++ language two-dimensional arrays are considered as an array of the one dimension array. Let’s consider the above example, In which row (3 )of the 2D array refer to a single dimension array and each element of the array is single dimension array of three integers.

 

accessing 2d array in c



Relationship between pointer and array in c

In C-language pointer and array are very close to each other, an array can be split in the form of the pointer. The name of the array is a pointer to its first element. So if acData is an array of character then “acData ” will be the address of its first element. You can also say that “acData” is similar to the &acData [0].

Below expression describe relation between array and pointer
acData [i] = *(acData +i) ————————->1D array in form of pointer
Now
acData [i][j] = *(acData [i]+j); ————————–> 2D array in form of 1D array and pointer.

Using the first expression we can also write
acData [i][j] = *(*(acData + i) + j) ———————->2D array in form of pointer

Array elements stored in a consecutive memory block we can access the elements of the array using the pointer.

See the below example,

Let suppose acBuffer is an array of character.

char acBuffer [] = {‘w’, ‘o’, ‘r’, ‘l’ ,’d’ };

To access the element of the array using the pointer create a pointer to the array element.

char * pcBuffer = NULL;

pcBuffer = acBuffer; // Assign the address first element
Now using the pointer you can access all elements of the array.

Let see the program example,

Relationship between pointer and array

Some important points related to array in c

  • The array is a collection of similar type of data and data is stored at a contiguous memory location.
  • Array elements access by index and the square brackets, array index should be integral types (no floating number).
  • Array name behaves like a constant pointer (R-value), that means when you try to assign an address to the array name then you will get the compiler error.For example,
    int aiData[5];
    int iVar;
    aiData = &iVar; (compiler error)
  • In c/c++ language array indices always start at 0 and end at one less than the size of the array.For example, an array which has 10 elements, indices start at 0 and end at 9. Because in array index work like the offset from the beginning address of the array, the first element of the array is at the beginning address of the array, so for the first element offset should be zero.
  • We can pass the array as an argument in the function, when we pass the array as an argument then it split into the pointer of its element.
    for example, if we passed the int aiData[4] as an argument, split into the pointer to an integer( int *).
  • We can return the array from the function in form of the pointer.
  • We can create an array of void* (generic pointer) but we can not create the array of void.
  • We can create the array of function pointer but we can not create the array of a function.
  • In the c language does not happen the bound checking of the array. This is a very big problem and some times it can create serious issues.
  • In c language, arrays are generally used in conjunction with loops, to perform some calculations (addition, subtraction or sorting etc) on all ( or some part ) of the elements in the array.