What is Array and Definition Representation of One Dimensional Array in Data Structure Using C and C++
What is Array & Definition, One Dimensional Array in Data Structure Using C and C++
An array is a collection of similar type of data items and each data item is called an element of array. The data type of the elements may be any valid data type like char, int or float. The elements of array share the same variable name but each element has a different index number known as subscript usually, the array of characters is called a ‘string’, whereas an array of ints or floats is called simply an array. All elements of any given array must be of the same type i.e. we cannot have an array of 10 numbers, of which 5 are ints and 5 are floats @ upbed.in
Array can be single dimensional or multidimensional. The number of subscripts determines the dimension of array. A one dimensional array has one subscript; two dimensional arrays have two subscripts and so on.
How to Declare Dimensional Array in Data Structure Using C and C++
Like other simple variables, array should also be declared before they are used in the program.
The syntax for declaration of an array is-:
data_type array_name[size] ;
Here array_name denotes the name of the array and it can be any valid C identifier, data_type is the data type of the elements of array. The size of the array specifies the number of elements that can be stored in the array. It may be a positive integer constant or constant integer expression.
For example-: int num[5];
Here we declare an array named num of integer type of size 5. ie this array can store maximum 5 elements of integer type.
These elements are stored in memory as follows.
0 1 2 3
num[0] num[1] num[2] num[3]
When the array is declared, the compiler allocates space in memory sufficient to hold all the elements of the array, so the compiler should know the size of array at the compile time.
Hence we can’t use variables for specifying the size of array in the declaration. The symbolic constant can be used to specify the size of array.
For Example:
#define SIZE 10 void main( ) { int name[ SIZE ]; }
How to Initialize one dimensional Array in Data Structure Using C and C++
After declaration, the elements of the local array have garbage value while the elements of global and static array are automatically initialized to zero. We can explicitly initialize arrays at the time of declaration.
The syntax for initialization of an array is:
data_type array_name[size] = { value1, value2……………valueN } ;
Here array_name is the name of the arary variable, size is the size of the array and value1, value2……..valueN are the constant values known as initializers, which are assigned to the array elements one after another.
for example: int marks[4] = { 20, 56, 61, 55 } ;
The values of the array elements after this initialization are:
marks[0] : 20, marks[1] : 56, marks[2] : 61, marks[3] : 55
How to Access one dimensional Array elements in Data Structure Using C and C++
The elements of an array can be accessed by specifying the array name followed by subscript in brackets. In C, the array subscripts starts from 0. Hence if there is an array of size 5 then the valid subscript will be from 0 to 4. The last valid subscript is sometimes know as the upper bound of the array and 0 is known as the lower bound of the array.
A subscripted array element is treated as any other variable in the program. We can store values in them, print their values or perform any operation that is valid for any simple variable of the same data type.
Example: let int ar[5] is an array.
then scanf(” %d ” , &ar[0] ); // input value into ar[0]
printf(” %d”, ar[2] ); // print value of ar[2]
ar[3] =25 ; // assign 25 into ar[3]
ar[2] + + ; // increment the value of ar[2] by 1