HOW TO ACCESS ELEMENTS IN ONE-DIMENSIONAL ARRAY

Dev Frank
6 min readFeb 1, 2024

--

Alright let’s take this scenario, imagine you have a row of colorful boxes, and each box has a toy inside. These boxes are like a magical line of spaces where we keep things, but each space is numbered. Now, if you want to get the toy from a specific box, you just need to know its number.

In programming, we have something similar called a one-dimensional array. It’s like a line of spaces (or boxes), and each space has a special number called an “index.” If you want to get something from a particular space in the array, you use its index.

So, let’s say you have an array of five spaces (like five boxes), and you want to know what’s in the third space. You’d say, “Hey, computer, show me what’s in space number three!” And just like that, you can access the toy (or value) in that specific spot.

So, in C programming access elements in a one-dimensional array in C, we use array indexing.

For instance, array_name[index] allows retrieval of the element at position index. Note that array indexing in C starts from 0, and you can access various elements by changing the index.

array_name[index]

array_name: This is the name of the array or list, indicating the collection of elements.

[index]: The square brackets denote indexing. The index represents the position of the element you want to access within the array. It is an integer value, usually starting from 0 for the first element.

For example, if you have an array named arr and you want to access the third element (index 2) from it, you would use arr[2].

int arr[5];

This expression evaluates to the value stored at the specified index within the array. The index starts from zero, and the last index is four, because if length is five the last index of the array will be four (i.e. Lenght-1).

Take for example:

#include <stdio.h>

int main() {
// Declare an array
int myArray[5] = {10, 20, 30, 40, 50};

// Access elements using indexing
int elementAtIndex2 = myArray[2]; // Access the third element (index 2)

// Print the accessed element
printf("Element at index 2: %d\n", elementAtIndex2);

return 0;
}

In the above example:

myArray[2]: This expression accesses the element at index 2 in the array myArray. Remember, C uses zero-based indexing, so the first element is at index 0, the second at index 1, and so on.

elementAtIndex2: This variable stores the value of the element at index 2.

printf(“Element at index 2: %d\n”, elementAtIndex2); : This prints the accessed element to the console.

You can use a similar approach to access elements in other positions within the array by changing the index inside the square brackets.

If you want to access the first element of an array:

We define the first index as zero;

arr[0] — — → Accessing the 1st element of an array

arr[1] — — → Accessing the 2ndelement of an array

arr[2] — — → Accessing the 3rd element of an array

arr[3] — — → Accessing the 4th element of an array

arr[4] — — → Accessing the 5th element of an array

NB: You should always remember that index starts from 0 and ends at Length-1

HOW TO INITIALIZE ONE DIMENSIONAL ARRAY

There are four(4) methods in initializing one dimensional array.

Method 1:

char myCharArray1[] = {'H', 'e', 'l', 'l', 'o', '\0'};

Here, there is no need to specify the datatype if it has already been declared somewhere else. If it has not been declared before, then you will have to declare the array. The elements in the array are seperated by a comma ( , ) and enclosed in curly braces ( { } ).

Method 2:

arr[] = {1, 2, 3, 4, 5};

This method is considered to be better than the previous one. In this method there’s no need to specify the length. Both are exactly the same. The advantage of not specifying the length is that you can add as many elements as you want.

Method 3:

int arr[5];

number[0] = 1;
number[1] = 2;
number[2] = 3;
number[3] = 4;
number[4] = 5;

In this method you have to specify the length, otherwise it will produce an error.

Now, after this initialization, the number array contains the values [1, 2, 3, 4, 5]. Each element in the array is assigned a specific value, and you can access these values using array indexing, as demonstrated in the subsequent code. For example, number[2] would give you the value 3.

Method 4:

include <stdio.h>

int main(void)
{
int myArray[5];

for(int i = 0; i < 5; i++)
{
printf("Enter a number: \n");
scanf(%d, &myArray[i]);
}

return 0;
}

Using this method, you should first of all declare the array with its length.

Then there’s a for loop, the variable “ i “ is been initialized to 0, since o is less than 5, we are going to enter the loop. The control prompts user to Enter a number when i = 0. The i becomes incremented to 1 which still makes the condition true, then again the control prompts user to Enter a number when i = 1. It does this till i = 5 which makes the condition false. Then it exits the loop.

Overall, this code collects five integers from the user and stores them in the myArray array, allowing the program to work with the entered data.

Zero Initialization

Zero Initialization is a process of setting all elements of an array to zero. In C, when you declare an array without explicitly providing initial values, the compiler automatically initializes the array elements to zero.

For example:

int myArray[5];  // Zero Initialization

In this case, all elements of myArray will be set to zero by default. This is particularly useful when you want to start with a clean slate, ensuring that the array contains predictable and known values.

It’s important to note that Zero Initialization behavior applies not only to integers but also to other data types. For instance, if you declare a character array, the elements will be initialized to null characters (‘\0’). This default initialization simplifies code and prevents potential issues related to uninitialized variables.

int arr[5] = {0};

This is another way to zero initialize an array.

int arr[5]: Declares an integer array named arr with a capacity to store 5 elements.
= {0} : Initializes all elements of the array to zero. This shorthand is allowed in C and ensures that each element of arr is set to 0.

int arr[5] = {0, 0, 0, 0, 0};

This is kind of the longhand pf the previous example of zero initializing an array.
In this case, the entire array arr is explicitly initialized with zeros. This is equivalent to setting each element to zero individually.

Both forms achieve the same result, ensuring that all elements in the array are set to zero. This is a convenient way to initialize an array when you want to start with a clean slate or ensure that all elements have a known initial value.

Partial Initialization:

Partial Initialization refers to initializing only a subset of elements in an array, leaving the remaining elements uninitialized. In C, you can partially initialize an array by specifying values for some elements while omitting others.

For example:

int arr[5] = {1, 2};

In this case, the first two elements of the array arr are explicitly set to 1 and 2, respectively. The remaining elements, if not explicitly initialized, will be implicitly initialized to zero. So, after the line above, the array arr would be {1, 2, 0, 0, 0}.

Partial Initialization is useful when you only need to set specific elements to known values, and the rest can be left to default initialization (usually zero).

--

--

Dev Frank

Passionate tech enthusiast, diving deep into the world of software engineering. Thrilled to share insights with the world. A Software engineering student.