1d one dimensional array in c programming.

  Declaring Single or One Dimensional Array in C programming.
Given Diagram is for Declaring 1D Array in C programming.
From Given diagram we can visualize a one dimensional array in C programming as a Single row to store a elements in continueously one after the another.
Array in C programming are used to store data or elements of same type in contiguous memory locations.
In C Programming Language we can declare the array like any other variable before using it in the Program.
Syntax of Array Declaration:
data_type array_name [size];

data_type :is a C Language Data type like int,char,float,long,double etc .

array_name[size] : array_name is the name of the array.

size: Size is the total number of elements in the array.The size is the Length of an array in C.. It must be specified at the time of declaration. It is the size of an array that is used to determine the memory required to store all of its elements.

Array Index: The index of an array is an integer number or value that identifies the position of a specific value or element within the array. An integer value that starts at zero for the very first element of the array and increments by one for each subsequent element.
For example, in a C array (int marks[5];)with 5 elements, the index of the first element is 0, the index of the second element is 1, the index of the third element is 2, and so on, up to the index of the fifth element, which is 4.
the last index 5 is NULL i.e. end of array.
The index plays very important role in C language to perform operations on array and access individual elements of the array.

Array Address: The address is the bytes of memeory occupied by array elements. The address of an array is the address of its first element.

From fig We can observe that
int marks[5];
array is declared by specifying its name, the type of its elements, and the size.
when array is declared in C programming , the compiler allocates the contiguous block of memory to the array name.
marks is a array variable name or vector variable and this variable can store 5 integer values.
The amount of memory occupied by the array variable marks is 10 bytes or 20 bytes( for 32-bit os int size is 2 byte and for 64 bit os 4 byte).
For example, the address of the first element in the array marks is 1000 for second element the address is 1002 and the address continues to 1010.
from the fig. we can observe that 10 byte of contiguous memory is allocated to store 5 integer number.
The five integer number(marks) stored will be having same name i.e marks. Then the main problem is how do we differentiate those five integer marks.
We can differentiate them with the index starting from 0 .
marks[0] means the first integer number,
marks[1] is the second integer and mark[4] is the fifth integer,
so we can easily differentiate or access using indexes.
Let say we want to store a marks obtained by a student1 is 68 at index 0 then we can do the same thing using expression.
marks[0]=68; Once we write the above assignment statement then it will store or assign 68 in the index position 0.
So that from above explanation we can conclude that, using the name and index we can access any of those elements present in a array.


Example to show address and index position of values in an array elements in C Programming:

Let us study the given C program from the given fig. which list the address and index value of an array .
int marks[5];
Array variables marks is declared as integer of size 5.
The array marks[5] occupies contiguous block of memory to store 5 integer values.
10 or 20 bytes of memory is occupied by the array variable marks.
for(unsigned int i=0;i<5;i++)
{
printf("\n Address at %d index is : %p",i,&marks[i]);
}

The for loop executes 5 times which gives the following output.
(Note: consider the base address of marks is 1000)




#include<stdio.h>
int main()
{
int marks[5];
for(unsigned int i=0;i<5;i++)
{
printf("\n Address at %d index is : %p",i,&marks[i]);
}
}
output:
Address at 0 index is : 1000
Address at 1 index is : 1002
.
.
Address at 4 index is :1010


Syntax Explanation :
The for loop executes five times and the output shown will be same as given above.