Array in C and C++ programming, Definition, int array, char array
An array is a data structure that can store one or more values. An array is a group of variables or a collection of variables of a similar data type. In C, an array contains values called elements stored in contiguous memory locations. An array has a fixed size N and it is indexed from 0 to N-1. An array's element is accessed by using an index.
For example, if you want to store 100 data values, what will you do? Declaring 100 individual variables makes it very difficult to manage. Instead of declaring 100 variables, we will have to declare one single variable with a length of 100. This single variable is referred to as an array. Array helps to easily manage and access the variables.
In C, an array can be of one dimension, two-dimension, or more.
Array Declaration
To declare a regular variable, we only have to specify the data type and variable name.
int mark;
To declare an array, we have to specify the data type, array name, and square brackets which indicate the maximum number of elements. The following are the general syntax of the array declaration.
data-typename array-name[ index-size];
Notice that there is no space between [ and name of the array.
Example
int mark[5];
This statement would cause contiguous space for five integer-type variables in the memory. This declaration is called a one-dimensional array. These five integers are called elements of an array and that is referred to in a program by writing:
mark[0] mark[1] mark[2] mark[3] mark[4]
Therefore, An array can be declared by specifying the data type, name of the array, and required number of elements.
Array Initialization
There are two ways in which an array is initialized. It can be done either at the time of declaration or each element separately.
int mark[5] = { 30,40,50,78,56};The above array can also be written as shown below:
int mark[5]; mark[0] = 30; mark[1] = 40; mark[2] = 50; mark[3] = 78; mark[4] = 56;
Assume an array is initialized in a single line. In this case, there is no need to specify the size in the square brackets. The array will be automatically allocated memory for all the elements in the list. The above statement will also be written as
//integer array in which each element is separated by comma. int mark[ ] = { 30,40,50,78,56};
If the number of values in the initializer list is less than the size of the array, the remaining elements of the array are initialized to zero.
The size of an array may be calculated by using the sizeof operator.
Example
// size will be assigned a value depending on the size of the array mark. //In this case, it is 5. int size=sizeof(mark);
In the case of a character array, it can be initialized by specifying the list of characters enclosed with single quotes within the brace{ }. Here each character is separated by using a comma.
//character array char name[ ] ={'H','E','L','L','O'};
This may also be initialized by using string constant enclosed by double-quotes.
char name[ ] = "HELLO";
The string initialization automatically appends a "\0" character, so the above array is of size 6, not 5. It is equivalent to writing,
char name[ ] 'H', 'E', 'L', 'L', 'O', '\0'}; //character array
An array element is accessed by using an index value. As mentioned earlier, an array index starts from 0 and ends at arraySize-1.
From the above exampleint number = mark[3];
This means that the 4th element of the mark array will be assigned to the variable number. So the value of number = 78.
Following is a program to display an integer array with its address.
#include<stdio.h> void main() { int a[5]={1,2,3,5,6}; int i; for(i=0;i < 5;i++) { printf("\n The value and address is:%d \t\t[%u]",a[i],&a[i]); } }
Output
The value and address is:1 [4294953920] The value and address is:2 [4294953924] The value and address is:3 [4294953928] The value and address is:5 [4294953932] The value and address is:6 [4294953936]Please note that the addresses may vary from machine to machine and from time to time
strcat() - String Concatenation strcmp() - String Compare
strcpy() - String Copy strlen() - String Length