Array is a collection of homogenous data, arranged in sequential format. Learning the concept of arrays in C is very important as it is the basic data structure. Here, in this section, we shall look into some very useful array programs to give you insight of how C programming language deals with arrays.
C Array Types
1.Single Dimensional Array :
- Single or One Dimensional array is used to represent and store data in a linear form.
- Array having only one subscript variable is called One-Dimensional array
- It is also called as Single Dimensional Array or Linear Array
Syntax :
<data-type> <array_name> [size];
Example of Single Dimensional Array :
int iarr[3] = {2, 3, 4}; char carr[20] = "c4learn" ; float farr[3] = {12.5,13.5,14.5} ;
2. Multi Dimensional Array :
- Array having more than one subscript variable is called Multi-Dimensional array.
- Multi Dimensional Array is also called as Matrix.
Syntax :
<data-type> <array_name> [row_subscript][column-subscript];
Example : Two Dimensional Array
int a[3][3] = { 1,2,3 5,6,7 8,9,0 };
Array within a Class
The array can be used as member variables in a class. The following class definition is valid.
const int size=10;
class array
{
int a[size];
public:
void setval(void);
void display(void);
};
The array variable a[] declared as private member of the class array can be used in the member function, like any other array variable. We can perform any operations on it. For instance, in the above class definition, the member function setval() sets the value of element of the array a[], and display() function displays the values. Similarly, we may use other member functions to perform any other operation on the array values.
const int size=10;
class array
{
int a[size];
public:
void setval(void);
void display(void);
};
The array variable a[] declared as private member of the class array can be used in the member function, like any other array variable. We can perform any operations on it. For instance, in the above class definition, the member function setval() sets the value of element of the array a[], and display() function displays the values. Similarly, we may use other member functions to perform any other operation on the array values.