C arrays

If you have read up to this part of the C language in w3camps, you are well acquainted with variables. But now it’s time to talk about composite data i.e. C array. Arrays are one of the great developments of its time. In a way, if we remove the array concept from the programming, you’re at a complete dead end. So one of the important things to become a good programmer is to learn array well.

C Arrays are data structures consisting of related data items of the same type. Arrays are “static” entities in that they remain the same size throughout program execution (they may, of course, be of automatic storage class and hence created and destroyed each time the blocks in which they’re defined are entered and exited).

Familiarity with array in C language

According to previous lessons, we know that in programming, variables are used to store information. But there are cases when there is a lot of information in one program, and an operation must be done on all of them. In this case, introducing many variables and performing operations on them will be very time-consuming and require long programs with similar commands.

To understand the issue, pay attention to the following example:
suppose it is necessary to read the output data of 50 temperature sensors connected to a microcontroller. This information needs to be stored after being read. This is where the usual variables that have been introduced so far are not the answer, and a new type of data should be found. Because if we want to use normal variables, we need to define 50 different and distinct names for the variables. Also, to perform mathematical operations, we must know everyone’s name. An array represents a collection of variables of both length and type under one name.

Advantages of using an array

  1. Easier declaration (declaration of one variable instead of tens and hundreds)
  2. The simplicity of working with them (the same mathematical operations are performed on them)
  3. More flexibility (they can be processed in different ways)
  4. Ease of understanding the program
  5. Reduce the size of the written program

One-dimensional arrays

As a result of a command in the form of int a, a variable named a is defined in the program, to which a two-byte memory cell is assigned. Now the question is whether it is possible to assign more cells to one name instead of one in the memory ?

The answer can be found in arrays. An array is a set of simple variables of the same type that are defined under a name in the program. The first type of array is one-dimensional; that is, only one row is defined for it. In the following, we will get acquainted with the method of defining the array.

Array definition in C language

To define an array, the type and name of the array must be mentioned during the definition, and after that, the number of elements of the array must be inserted inside a pair of square brackets, i.e., []. In the box below, you can see the definition:

<array type> <array name> [<array index>];

In an array with n elements, the index is defined from zero to n-1, and all these elements are of the same type. For example, for an array with 5 cells, the index range is from 0 to 4. Consider the following example:

char array[7];
int sample[10];
  1. On line one, an array of type char called array with 7 cells is defined.
  2. On the second line, an array of type int named sample is defined, which has 10 cells.

Note: The size or number of array houses must be specified at the time of its definition

Value Assignment in C arrays

Accessing and assigning values ​​to the elements of arrays is done through the subscript operator, whose symbol is a pair of square brackets and is placed on the right side of the array name. The desired cell number of the array, which is also called the array index, is placed inside the pair of square brackets. Pay attention to the following example:

int array[4] = {3, 6, 10, 15};

In the array of 4 members above, specified by the name array:

  1. The number 3 is placed in array[0].
  2. The number 6 is placed in array[1].
  3. The number 10 is placed in array[2].
  4. The number 15 is placed in array[3].

Arrays can also be set in the following style:

array[0] = 3;
array[1] = 6;
array[2] = 10;
array[3] = 15;

For a better understanding of the definition of the array, pay attention to the following figure:

c arrays

In the above figure, a one-dimensional array of type int with 7 cells is defined. The index of the array starts from 0 and goes up to 6. Also, according to the figure, we know what number is stored in each cell. To access and assign array cells, there is another approach using c pointers which we will discuss later on.

Question: How many bytes is the length of each cell?

Two-dimensional arrays

So far, we have discussed arrays with one index. In other words, all their cells are in one dimension (1D). There are other arrays that have two indexes. The cells of these arrays are in two dimensions(2D). Just like the cells of a table that you solve in the newspaper. As a result of the two-dimensional array, a matrix has rows and columns. Each cell of this matrix is ​​a variable and is specified by row and column indexes.

The rules and how to introduce the two-dimensional array are exactly the same as the regular arrays, except that the number of rows and columns must be specified. To define a two-dimensional array, proceed as follows:

<array type> <array name>[<row numbers>][<column numbers>];

Pay attention to the following example:

int array[2][4];

In this example, a two-dimensional array of type int called “array” is defined, which has 2 rows and 4 columns. It has a total of 8 cells. Now we initialize the above example:

int array[2][4] = { {1,2,3,4}, {5,6,7,8} };

Numbers 1, 2, 3, and 4 are placed in cells 0 to 3 of the first row, and numbers 5, 6, 7, and 8 are placed in cells 0 to 3 of the second row.

array[0][0] =1      array[0][1]=2       array[0][2]=3       array[0][3]=4
array[1][0] =5      array[1][1]=6       array[1][2]=7       array[1][3]=8

Two-dimensional array members can also participate in calculations by themselves.

Loops should be used to initialize, read, or analyze arrays during the program. We discuss C for and while loops in the future.

Was this helpful?
[0]
Scroll to Top
Scroll to Top