Memory representation of an array:
All the array elements in C occupy sequential block of memory spaces and each memory block is of same size. For example, int num[6]; /* An integer array of 6 elements*/ it's length is 6, so this array will get 6 sequential memory block in the RAM and each memory block has a unique address in byte.
There is a difference of 4 among the address of subsequent neighbors, since an integer type array holds 4 bytes of memory, therefor each block will occupy memory of 4 bytes. For example, address of 1st block is 1000, so 2nd block will be 1004, 3rd block 1008, 4rth block 10012, 5th block 1016, and last block will be 1020 as you can see in below diagram.
Memory representation of array |
Array Indexing:
The location of an elements in an array is represented by numerical value which is called array index. In programming languages, the indexes always start with 0, meaning that the first elements has index 0, and second has index 1, third has index 2, similarly it continue through the natural numbers until the last index (N-1). Where N represents the length of an array. Let's have a look at the below example:
Note: The elements of an array share the same variable name but each element has its own unique index number (also known as a subscript) see the above diagram.
Accessing elements of an array:
you can use index number (subscript) to access any elements stored in array. Since we know that, The elements of an array share the same variable name but each element has its own unique index number (also known as a subscript) see array indexing. This is how you can access the array elements.
/* Array Declaration with initialization */
int num[] = {2, 4, 6, 8, 10, 12};
num[0] = 2; // 1st element of array, since num[0] represent 1st index
num[3] = 8; // 4th element of array, since num[3] represent 4th index
/* Last element of array, since num[5] represent 6th index i.e last index of array.*/
num[5] = 12;
Let's understand with below example
Example of Array: Program to find average of 4 numbers.
#include <stdio.h>
int main()
{
int avg = 0;
int sum =0;
int x=0;
/* Array- declaration – length 4 */
int num[4];
/* We are using a for loop to traverse through the array
* while storing the entered values in the array
*/
for (x=0; x<4; x++)
{
printf("Enter number %d \n", (x+1));
scanf("%d", &num[x]);
}
/* Displaying elements of array */
for (x=0; x<4; x++)
{
sum = sum+num[x];
}
avg = sum/4;
printf("Average of entered number is: %d", avg);
return 0;
}
Output
Enter number 1
10
Enter number 2
10
Enter number 3
20
Enter number 4
40
Average of entered number is: 20
How to store elements in array?
Here, we are traversing the array from 0 to 9 since length of array is 10. Inside the loop we are taking input form user of any 10 numbers. All the input numbers will be stored in corresponding array using scnaf() function. Look at the below statements taken from the above example.
/* We are using a for loop to traverse through the array
* while storing the entered values in the array */
for (x=0; x<4; x++)
{
printf("Enter number %d \n", (x+1));
scanf("%d", &num[x]);
}
How to display elements from an array?
Again you need to run a for loop to display the stored elements of an array. Look at the below statements taken from the above example.
/* Displaying elements of array */
for (x=0; x<4; x++)
{
sum = sum + num[x];
}
How to calculate the size of an array?
You can calculate the size of an array in bytes by using sizeof operator.
Example: Size of array in bytes using sizeof() operator
/* Calculating size of an int array using sizeof operator */
#include <stdio.h>
int main()
{
int arr[6] = {2, 4, 6, 8, 10, 12};
printf("size of arr = %d bytes", sizeof(arr));
return 0;
}
Output
Size of arr = 24 bytes
How to calculate the length of an array?
There are various technique to find length of array in C, by using sizeof() operator and without using sizeof() operator. Let's see the both technique.
Length of array = sizeof (arr) / sizeof (arr[0])
Example 1: Length of an array using sizeof operator
/* Calculating the length of int array using sizeof operator */
#include <stdio.h>
int main()
{
int arr[6] = {2, 4, 6, 8, 10, 12};
printf("Length of arr = %d", sizeof(arr) / sizeof(arr[0]));
return 0;
}
Output
Length of arr = 6
Example 2: Length of an array without sizeof() operator
/* Calculating the length of int array without sizeof() operator */
#include <stdio.h>
int main()
{
int arr[6] = {2, 4, 6, 8, 10, 12};
int i = 0;
while(arr[i] !=12)
{
i++;
}
printf("Length of arr = %d",i+1);
return 0;
}
Output
Length of arr = 6
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.