top of page

A Comprehensive Guide to Arrays in C

Introduction


An array is a type of data structure that can store many values of the same type in contiguous memory locations and within the same variable.



School or gym lockers aren't so different from arrays in that they:

  • are usually contiguously placed

  • are identically-sized blocks of space

  • can each store a certain amount of items

  • usually store the same type of items (datatypes)

  • can be accessed by number (index)


Array Indexing


  • The convention for computer scientists to start from 0 comes from array indexing

    • arrays begin with the 0th element and end with the n-1th in an array of size n

  • C allows you to access elements beyond the stated range (0 to n-1)

    • sometimes junk values are returned (previously-used data floating around)

    • sometimes you may encounter a Segmentation Fault error


Declaring, Initializing, and Filling Arrays


data-type name[size];
  • data-type: this will dictate which one type of values are stored in your array (int, char, float, etc.)

  • name: the variable name of your array

  • size: how many total elements your array will store


For instance:

int digits[10];

One way to fill this array would be through directly initializing digits with a set of values:

int digits[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

Another way could be through indexing, using a square-bracket notation:

int digits[10];

digits[0] = 0;
digits[1] = 1;
digits[2] = 2;
// and so on...

This essentially goes into the 0th slot in our array and fills it with the given value.


Perhaps a more efficient and dynamic way to fill this array would be through a loop:

for (int i = 0; i < 10; i++)
{
	digits[i] = i;
}

Multi-Dimensional Arrays


Interestingly, arrays are not restricted to a single dimension. Oftentimes, 2D arrays are used to simulate boardgames like tic-tac-toe or battleship in programming.


bool ticTacToe[5][5];

You can also think of 2D arrays like arrays made up of individual arrays, but in memory, multi-dimensional arrays are simply stored as one large one-dimensional array (in our case, 25 slots of memory long).


Copying Arrays


Intuitively, you might try to assign one array to a variable in order to "copy" it as such:


int arr[3] = { 1, 3, 4 };
int cpy[3];

cpy = arr;

Unfortunately, this does not work with arrays in C. Copying values from arr into cpy would require accessing each individual values in arr and using indexing to assign them to cpy like so:


int arr[3] = { 1, 3, 4 };
int cpy[3];

for (int i = 0; i < 3; i++)
{
	cpy[i] = arr[i];
}

To make this loop more dynamic such that it could work with an array of any size, we could modify our code as such:


size_t length = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < length ; i++)
{
	cpy[i] = arr[i];
}

*size_t is just a datatype similar to int, but it only assigns int values greater than or equal to 0


Ultimately, the reason you can't copy arrays into variables is because the variable does not represent the array itself, but just a pointer to the first value in that array, which can get slightly complicated.


Passing by Reference


Unlike with variables passed into functions that are passed by value, arrays are passed by reference.


This means that instead of a function creating a local copy of the array, the array itself is passed into the function and can be modified.


This makes sense because arrays are so large that copying them would be a pretty significant waste of system resources.


*Also, when arrays are passed into functions, they are treated as pointers to the address of the first element. Since the actual address is being passed and not simply a variable, the contents within that address will be modified.


Final Thoughts


I hope this guide has shed some light on how arrays work and how you might find them useful in your own programs. Thanks for reading!

Comments


bottom of page