ARRAYS IN C

Dev Frank
4 min readJan 29, 2024

--

ARRAYS are collections of homogenous data items which are stored in contiguous memory locations. The elements are accessed by their index or position in the array.

It is a data structure containing a number of data values (all of which are if the same type).

Imagine an array as a large chunk of memory divided into smaller block of memory and each block is capable of storing a data value if same type and all of them are arranged in a sequential manner.

ONE DIMENSIONAL ARRAY

This is how one dimensional array looks like:

It is a large chunk of memory a single. It is in a single row divided into number of blocks and each block is capable of storing data values and all of them must be if the same data type.

Declaration Of One Dimensional Array

To declare an array, you specify the data type of its elements and provide a name for the array along with the size in square brackets.

Syntax

datatype variable_name[number of elements];

An array of integers can be declared as follows

int arr[5];

The data type of this array is int the name of the array is arr and the number of this array is 5.

This means the compiler will allocate a contiguous block of memory of size 20 or 10 (5 * sizeof (int)), because the array is if int data type which is either 4 bytes or 2 bytes.

So therefore, the compiler 20 or 10 bytes, memory in order to store the data values.

The length of an array can be specified bely any positive integer constant expression.

Examples

1. Integer Array:
This declares an array named integerArray with a size of 5 and initializes it with the values 1, 2, 3, 4, and 5.

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

2. Float Array:
Here, floatArray is an array of size 3 containing the floating-point numbers 2.5, 3.14, and 1.8.

float floatArray[3] = {2.5, 3.14, 1.8};

3. Character Array:

char charArray[10] = {’H’, 'e’, 'l’, 'l’, 'o’, ' ', 'C’, '!’, '\0’};

This declares a character array charArray with a size of 10 and initializes it with the characters 'H’, 'e’, 'l’, 'l’, 'o’, a space, 'C’, '!’, and a null terminator (`’\0’`), making it a string.

4. Double Array:

double doubleArray[8] = {0.5, 1.0, 2.75, 3.33, 4.2, 5.6, 6.8, 7.1};

5. Boolean Array (using int to represent boolean values):

int boolArray[6] = {1, 0, 1, 1, 0, 1};

In C, integers are often used to represent boolean values, where 1 is true and 0 is false. Here, boolArray is initialized with a sequence of true and false values.

Each example demonstrates the declaration and initialization of an array with specific data types and values in C.

Specifying the length of an array using MACRO is considered to be an excellent practice

#define N 10    //This is  a macro

int main(void)
{

int array[N];
}

USING MACROS TO SPECIFY THE LENGTH OF AN ARRAY

In C, you cannot directly specify the length of an array using a macro in the traditional sense, as the size of an array needs to be a compile-time constant. However, you can use a macro to define a constant that represents the size of an array, and then use that constant in the array declaration. This approach provides flexibility and helps maintainability.

Here's an example:

#include <stdio.h>
#define ARRAY_LENGTH 5


int main() {
int numbers[ARRAY_LENGTH] = {1, 2, 3, 4, 5};

for (int i = 0; i < ARRAY_LENGTH; ++i) {
printf("%d ", numbers[i]);
}

return 0;
}

In this example, the macro ARRAY_LENGTH is defined with the value 5. Then, the array `numbers` is declared with the specified length using this macro.

Using a macro for the array length has benefits:
1. Maintainability: If you need to change the array size, you only need to modify the macro’s value, and it automatically updates the array size wherever the macro is used.

2. **Readability:** The use of a named constant makes the code more readable and self-explanatory.

Keep in mind that macros are replaced by their values during the preprocessing stage, so using a macro for array size provides a convenient way to manage and adjust array sizes consistently across your codebase.

TRY THIS

Which is / are incorrect, and why?

Interact in the comment section

int my_Variable_Name[5];
float Array[10] = {’H’, 'e’, 'l’, 'l’, 'o’, ' ', 'C’, '!’, '\0’};
char alphabet[8] = {’x’, 'v’, 'l’, 'y’, 'o’, 'q ', 'k, ’\0’};
double doubleArray[8] = {30.5, 9.0, 65.75, 3.33, 465.2, 5.6, 6.8, 7.1};
int number[-5];
int boolArray[6] = {1, y, 1, 1, 0, d};

--

--

Dev Frank

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