C one-dimensional array

Declaration of One Dimensional Array:

For example:

Above example is an one dimensional array. Here the name of an array is arr with array size 5. int specifies the type of variable. The [5] tells how many elements in the array. The array_size must be an integer constant greater than zero.

The array_name itself is a constant pointer pointing to the base address of an array. The base address of the array is the address of the first element of an array.

Initialisation of One-Dimensional Array:

Arrays can be initialized at declaration time. You can initialize an element of an array, one by one or in a single statement. The number of an element inside of braces { } should not be greater than array_size.

Initializing each element separately in a loop:

Points to remember:


  • Here array size is 5 and number of elements is 2, so remaining elements are set to zero.
    This phenomenon is called as partial initialization of array.

  • Mentioning the dimension of the array is optional.
  • To find the size of an entire array, sizeof is used. Therefore, in above example sizeof(arr) is 20 bytes.
    This is because array elements are 5 of integer type therefore 5*4=20.
    The size of each element, sizeof(arr[0]) is 4 bytes because array is of integer type.

  • The array_size should not be a variable name.

Accessing array elements using subscript:

An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array.

For example:

one dimensional array
values are of int data type, whereas addresses are of int* type.

&arr[0] :- 100 – address of the first element of the array.
&arr[1] :- 104 – address of the second element of the array.
&arr[2] :- 108 – address of the third element of the array.
&arr[3] :- 112 – address of the forth element of the array.
&arr[4] :- 116 – address of the fifth element of the array.

arr[0] :- 1 – retrive value of the first element of the array.
arr[1] :- 2 – retrive value of the second element of the array.
arr[2] :- 3 – retrive value of the third element of the array.
arr[3] :- 4 – retrive value of the forth element of the array.
arr[4] :- 5 – retrive value of the fifth element of the array.

scanf(“%d”,&arr[2]); //statement to insert value in the third element of array arr.
printf(“%d”,arr[0]); //statement to print first element of an array.

C Array Example:
C program to find the sum of marks of n students using array subscript notation

Output:

Accessing array with Pointer:

We can also access elements of an array using pointers. Name of the array itself is a pointer pointing to the base address of an array.

Suppose for example:

Now by using following terms in the program, we can access array elements.

array

address of an element can be written as:
address = (array_name + index*sizeof(data_type))
& arr[1]=(arr + 1 x 4)=104

(arr+0) :- 100 – address of the first element of the array.
(arr+1) :- 104 – address of the second element of the array.
(arr+2) :- 108 – address of the third element of the array.

*(arr+0) :- ‘1’ -retrieve value of the first element of the array.
*(arr+1) :- ‘2’ -retrieve value of the second element of the array.
*(arr+2) :- ‘3’ -retrieve value of the third element of the array.

scanf(“%d”,(arr+2)); //statement to insert value in the third element of array arr[].
printf(“%d”,(arr+0); //statement to print first element of an array.

Here arr+1 gives an address of the second element of the array arr. Because base address of arr is 100 and 1 is index(i.e 4 bytes) therefore 100+4=104

array

For example:

C Array Example:
C program to find the sum of marks of n students using array using the pointer.

Output:

 

Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.