Array Definition in C
Arrays
An array groups more than one similar typed value in consecutive memory regions. The comparable array name can access an array's elements by supplying the index value name as the memory address.
In C, there are three different types of arrays:
- 1D arrays
- 2D arrays
- Multi-dimensional arrays
One Dimensional Array in C –
In C, a one-dimensional array is represented by a single row that holds all elements. The components are all kept near one another in memory. We will now examine array elements' declaration, initialization, and access.
Declaring an Array-
Any data type and name can be used when declaring an array with just one dimension, exactly like when naming a variable with a random value.
Syntax:
data_type arr_name[size_of_the_array];
Initializing an Array-
An array that is not initialized will have garbage values. However, we have the option to initialize them explicitly when making their declaration.
Syntax:
data_type arr_name[size] = {val1, val2, val3, val4,…};
Parameterized values in this context are constant values that are separated by commas.
We can skip including the array's size in square brackets if we explicitly initialize array members in the given list of items at the initial stage of the declaration. In such a scenario, the array size will be calculated according to the size of the element list.
Example-
int val[ ] = {17,24,65,68};
We could easily use the below way of representing If we wanted to initialize every element of an integer array to zero.
int name_of_array[size_of_array] = {0};
Way To Access the Array Elements –
If you want to access array elements just write the index number of the item inside the "[ ]". Indexing an array begins with 0 and finishes with size-1. The compiler will return some trash value instead of an error notice if we attempt to access an array element outside the permitted range.
Syntax:
<array_name>[index_val];
Example-
#include <stdio.h>
int main(){
char letters[4]= {'p','b','c','k'};
for(int i=0;i<4;i++){
printf("%c ",letters[i]);
}
return 0;
}
Output-
Initializing at Compile-Time-
Static initialization is another term for compile-time initialization. Each time we declare an array implicitly, items of the array are initialized.
Syntax:
<data_type> <arr_name>[size_of_array]={items of an array};
Example-
int values[4] = {18,19,20,21};
C-program –
#include <stdio.h>
int main(){
char letters[4]= {'p','b','c','k'};
for(int i=0;i<4;i++){
printf("letters[%d] = %c\n",i,letters[i]);
}
}
Output-
Initializing at Run-Time-
Dynamic initialization is another term for run-time initialization. After successfully compiling the program, array items are initialized during run-time.
Example-
scanf("%d", &letters[0]);
C-program-
#include <stdio.h>
int main(){
char letters[4];
//taking input from the user
for(int i=0;i<4;i++){
scanf("%c",&letters[i]);
}
//printing elements to the output screen
for(int i;i<4;i++){
printf("%c ",letters[i]);
}
return 0;
}
Output-
In the above C code, we just declared the array "letters" with a size of 4. The user is then prompted to input the array values within a loop to initialize it, following the compilation process of the code. Ultimately, we printed its values using index-based access.
C Code for 1-D Arrays –
If we have two arrays, nums_1, and nums_2, one of which is initialized and the other of which has just been defined, and say that we need to copy the elements from nums_1 to nums_2, then we can't just write:
int nums_1[4] = {100,200,300,400};
int nums_2[4];
array2 = array1;
The above code will give you an error.
Here is some code that shows how to duplicate a 1-D array's elements.
#include <stdio.h>
int main() {
int nums_1[4] = {100,200,300,400};
int nums_2[4];
printf("copying the elements from nums_1 to nums_2\n");
printf("nums_1 elements = ");
for (int i = 0; i < 4; i++) {
printf("%d ", nums_1[i]);
nums_2[i] = nums_1[i];
}
printf("\nElements of nums_2 after copying : ");
for (int i = 0; i < 4; i++) {
printf("%d ", nums_2[i]);
}
return 0;
}
Output-
We've utilized two arrays in this C program: nums_1 and nums_2. We are assigning the values from nums_1 to nums_2 over a loop to demonstrate the idea of copying array items. nums_1 was initialized at the moment of declaration. Finally, we displayed the items of array.
Key Takeaways:
- In C, arrays are comprised of data types with related data-type members.
- When declaring 1-D in C, we must include the data type, variable name for the array, and size of the array in square brackets.
- C supports static initialization (at compile time) and dynamic initialization (during run time) for 1-D arrays.
- An array's components are all kept close to one another in memory, making it possible to retrieve them using their unique index numbers.